C#Assembly中的CreateInstance和Activator 中的CreateInstance
创始人
2024-11-18 18:35:18
0

        Assembly中的CreateInstance,使用区分大小写的搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。它最后调用的是Activator中的CreateInstance方法。

    [Serializable]     [ClassInterface(ClassInterfaceType.None)]     [ComDefaultInterface(typeof(_Assembly))]     [ComVisible(true)]     [__DynamicallyInvokable]     [PermissionSet(SecurityAction.InheritanceDemand, Unrestricted = true)]     public abstract class Assembly : _Assembly, IEvidenceFactory, ICustomAttributeProvider, ISerializable {         public object CreateInstance(string typeName)         {             return CreateInstance(typeName, ignoreCase: false, BindingFlags.Instance | BindingFlags.Public, null, null, null, null);         }          public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)         {             Type type = GetType(typeName, throwOnError: false, ignoreCase);             if (type == null)             {                 return null;             }              return Activator.CreateInstance(type, bindingAttr, binder, args, culture, activationAttributes);         } }

         Activator 中的CreateInstance。

    [ClassInterface(ClassInterfaceType.None)]     [ComDefaultInterface(typeof(_Activator))]     [ComVisible(true)]     [__DynamicallyInvokable]     public sealed class Activator : _Activator     {               [MethodImpl(MethodImplOptions.NoInlining)]         [SecuritySafeCritical]         public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)         {             if ((object)type == null)             {                 throw new ArgumentNullException("type");             }              if (type is TypeBuilder)             {                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));             }              if ((bindingAttr & (BindingFlags)255) == 0)             {                 bindingAttr |= BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;             }              if (activationAttributes != null && activationAttributes.Length != 0)             {                 if (!type.IsMarshalByRef)                 {                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));                 }                  if (!type.IsContextful && (activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute)))                 {                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));                 }             }              RuntimeType runtimeType = type.UnderlyingSystemType as RuntimeType;             if (runtimeType == null)             {                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");             }              StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;             return runtimeType.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes, ref stackMark);         }  }

         示例:

        以下示例定义类 Person 并调用 CreateInstance(String) 方法来实例化它。

using System; using System.Reflection; using Contoso.Libraries;  namespace Contoso.Libraries {    public class Person    {       private string _name;        public Person()       { }        public Person(string name)       {          this._name = name;       }        public string Name       { get { return this._name; }         set { this._name = value; } }        public override string ToString()       {          return this._name;       }    } }  public class Example {    public static void Main()    {       Assembly assem = typeof(Person).Assembly;       Person p = (Person) assem.CreateInstance("Contoso.Libraries.Person");       if (! (p == null)) {          p.Name = "John";          Console.WriteLine("Instantiated a {0} object whose value is '{1}'",                            p.GetType().Name, p);       }       else {          Console.WriteLine("Unable to instantiate a Person object.");       }    } } // The example displays the following output: //        Instantiated a Person object whose value is 'John'

 

相关内容

热门资讯

科普!微信炸金花在哪里充值房卡... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!怎么购买炸金花房卡链接,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!微信金花房卡如何购买,微... 微信游戏中心:斗牛房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科普!金花链接房卡找谁买,微信... 微信游戏中心:牛牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
科普!微信金花房卡链接怎么弄的... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
科普!金花从哪里购买房卡,微信... 微信游戏中心:斗牛房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科普!微信上玩炸金花房卡怎么买... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信里玩炸金花链接房卡,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!怎么购买金花房卡链接,拼... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!金花房卡在哪里买,斗牛房... 微信游戏中心:斗牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科普!如何购买金花链接房卡,微... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
科普!微信如何创建炸金花房间链... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信金花房卡有没有购买,... 微信游戏中心:牛牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
科普!炸金花房卡链接在哪弄的,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!微信玩炸金花房卡哪里购买... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!有没有卖金花链接房卡,怎... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!微信金花房卡购买方式,微... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
科普!炸金花在哪买房卡,微信上... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!怎么开炸金花房卡链接,拼... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!微信好友金花房卡跟谁买,... 微信游戏中心:牌九房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牌九房卡”,...