The Labs \ Source Viewer \ SSCLI \ System.ComponentModel.Design.Serialization \ InstanceDescriptor

  1. //------------------------------------------------------------------------------
  2. // <copyright file="InstanceDescriptor.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel.Design.Serialization
  16. {
  17.    
  18.     using System;
  19.     using System.Collections;
  20.     using System.Diagnostics;
  21.     using System.Reflection;
  22.     using System.Security.Permissions;
  23.    
  24.     /// <devdoc>
  25.     /// EventArgs for the ResolveNameEventHandler. This event is used
  26.     /// by the serialization process to match a name to an object
  27.     /// instance.
  28.     /// </devdoc>
  29.     [HostProtection(SharedState = true)]
  30.     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name = "FullTrust")]
  31.     public sealed class InstanceDescriptor
  32.     {
  33.         private MemberInfo member;
  34.         private ICollection arguments;
  35.         private bool isComplete;
  36.        
  37.         /// <devdoc>
  38.         /// Creates a new InstanceDescriptor.
  39.         /// </devdoc>
  40.         public InstanceDescriptor(MemberInfo member, ICollection arguments) : this(member, arguments, true)
  41.         {
  42.         }
  43.        
  44.         /// <devdoc>
  45.         /// Creates a new InstanceDescriptor.
  46.         /// </devdoc>
  47.         public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
  48.         {
  49.             this.member = member;
  50.             this.isComplete = isComplete;
  51.            
  52.             if (arguments == null) {
  53.                 this.arguments = new object[0];
  54.             }
  55.             else {
  56.                 object[] args = new object[arguments.Count];
  57.                 arguments.CopyTo(args, 0);
  58.                 this.arguments = args;
  59.             }
  60.            
  61.             if (member is FieldInfo) {
  62.                 FieldInfo fi = (FieldInfo)member;
  63.                 if (!fi.IsStatic) {
  64.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
  65.                 }
  66.                 if (this.arguments.Count != 0) {
  67.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
  68.                 }
  69.             }
  70.             else if (member is ConstructorInfo) {
  71.                 ConstructorInfo ci = (ConstructorInfo)member;
  72.                 if (ci.IsStatic) {
  73.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorCannotBeStatic));
  74.                 }
  75.                 if (this.arguments.Count != ci.GetParameters().Length) {
  76.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
  77.                 }
  78.             }
  79.             else if (member is MethodInfo) {
  80.                 MethodInfo mi = (MethodInfo)member;
  81.                 if (!mi.IsStatic) {
  82.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
  83.                 }
  84.                 if (this.arguments.Count != mi.GetParameters().Length) {
  85.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorLengthMismatch));
  86.                 }
  87.             }
  88.             else if (member is PropertyInfo) {
  89.                 PropertyInfo pi = (PropertyInfo)member;
  90.                 if (!pi.CanRead) {
  91.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeReadable));
  92.                 }
  93.                 MethodInfo mi = pi.GetGetMethod();
  94.                 if (mi != null && !mi.IsStatic) {
  95.                     throw new ArgumentException(SR.GetString(SR.InstanceDescriptorMustBeStatic));
  96.                 }
  97.             }
  98.         }
  99.        
  100.         /// <devdoc>
  101.         /// The collection of arguments that should be passed to
  102.         /// MemberInfo in order to create an instance.
  103.         /// </devdoc>
  104.         public ICollection Arguments {
  105.             get { return arguments; }
  106.         }
  107.        
  108.         /// <devdoc>
  109.         /// Determines if the contents of this instance descriptor completely identify the instance.
  110.         /// This will normally be the case, but some objects may be too complex for a single method
  111.         /// or constructor to represent. IsComplete can be used to identify these objects and take
  112.         /// additional steps to further describe their state.
  113.         /// </devdoc>
  114.         public bool IsComplete {
  115.             get { return isComplete; }
  116.         }
  117.        
  118.         /// <devdoc>
  119.         /// The MemberInfo object that was passed into the constructor
  120.         /// of this InstanceDescriptor.
  121.         /// </devdoc>
  122.         public MemberInfo MemberInfo {
  123.             get { return member; }
  124.         }
  125.        
  126.         /// <devdoc>
  127.         /// Invokes this instance descriptor, returning the object
  128.         /// the descriptor describes.
  129.         /// </devdoc>
  130.         public object Invoke()
  131.         {
  132.             object[] translatedArguments = new object[arguments.Count];
  133.             arguments.CopyTo(translatedArguments, 0);
  134.            
  135.             // Instance descriptors can contain other instance
  136.             // descriptors. Translate them if necessary.
  137.             //
  138.             for (int i = 0; i < translatedArguments.Length; i++) {
  139.                 if (translatedArguments[i] is InstanceDescriptor) {
  140.                     translatedArguments[i] = ((InstanceDescriptor)translatedArguments[i]).Invoke();
  141.                 }
  142.             }
  143.            
  144.             if (member is ConstructorInfo) {
  145.                 return ((ConstructorInfo)member).Invoke(translatedArguments);
  146.             }
  147.             else if (member is MethodInfo) {
  148.                 return ((MethodInfo)member).Invoke(null, translatedArguments);
  149.             }
  150.             else if (member is PropertyInfo) {
  151.                 return ((PropertyInfo)member).GetValue(null, translatedArguments);
  152.             }
  153.             else if (member is FieldInfo) {
  154.                 return ((FieldInfo)member).GetValue(null);
  155.             }
  156.             else {
  157.                 Debug.Fail("Unrecognized reflection type in instance descriptor: " + member.GetType().Name);
  158.             }
  159.            
  160.             return null;
  161.         }
  162.     }
  163. }

Developer Fusion