当前位置: 首页>>代码示例>>C#>>正文


C# Assembly.CreateInstance方法代码示例

本文整理汇总了C#中System.Reflection.Assembly.CreateInstance方法的典型用法代码示例。如果您正苦于以下问题:C# Assembly.CreateInstance方法的具体用法?C# Assembly.CreateInstance怎么用?C# Assembly.CreateInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Assembly的用法示例。


在下文中一共展示了Assembly.CreateInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateClassInstance

		/// ------------------------------------------------------------------------------------
		/// <summary>
		///
		/// </summary>
		/// ------------------------------------------------------------------------------------
		public static object CreateClassInstance(Assembly assembly, string className, object[] args)
		{
			try
			{
				// First, take a stab at creating the instance with the specified name.
				object instance = assembly.CreateInstance(className, false,
					BindingFlags.CreateInstance, null, args, null, null);

				if (instance != null)
					return instance;

				Type[] types = assembly.GetTypes();

				// At this point, we know we failed to instantiate a class with the
				// specified name, so try to find a type with that name and attempt
				// to instantiate the class using the full namespace.
				foreach (Type type in types)
				{
					if (type.Name == className)
					{
						return assembly.CreateInstance(type.FullName, false,
							BindingFlags.CreateInstance, null, args, null, null);
					}
				}
			}
			catch { }

			return null;
		}
开发者ID:JohnThomson,项目名称:libpalaso,代码行数:34,代码来源:ReflectionHelper.cs

示例2: DataAccessProviderFactory

 static DataAccessProviderFactory()
 {
     string providerName = ConfigurationManager.AppSettings["DataProvider"];
     string providerFactoryName = ConfigurationManager.AppSettings["DataProviderFactory"];
     activeProvider = Assembly.Load(providerName);
     activeDataProviderFactory = (IDataProviderFactory)activeProvider.CreateInstance(providerFactoryName);
 }
开发者ID:jomamorales,项目名称:IMAP,代码行数:7,代码来源:DataAccessProviderFactory.cs

示例3: TryLoadClass

        //=======================================================================
        //=======================================================================
        public static bool TryLoadClass(Assembly assembly, string className, out string message, out object instance)
        {
            instance = null;

            if (assembly == null)
            {
                message = "Assembly must not be null";
                return false;
            }

            try
            {
                instance = assembly.CreateInstance(className);
                message = "Class loaded successfully";
                return true;
            }
            catch (MissingMethodException)
            {
                message = "Error loading class, constructor not found.";
                return false;
            }
            catch (Exception ex)
            {
                message = ("Class load error: " + ex.Message);
                return false;
            }
        }
开发者ID:bluetulip89,项目名称:robotitalk,代码行数:29,代码来源:AssemblyManager.cs

示例4: GetCommandsFromAssembly

 private IEnumerable<ICommand> GetCommandsFromAssembly(Assembly assembly)
 {
     try
     {
         return assembly
             .GetTypes()
             .Where(type =>
                        {
                            return type.IsClass;
                        })
             .Where(type =>
                        {
                            return type.GetInterface("ShellMe.CommandLine.CommandHandling.ICommand") != null;
                        })
             .Select(type =>
                         {
                             try
                             {
                                 return (ICommand) assembly.CreateInstance(type.ToString());
                             }
                             catch (Exception)
                             {
                                 return null;
                             }
                         })
             .Where(command => command != null);
     }
     catch (Exception exception)
     {
         return Enumerable.Empty<ICommand>();
     }
 } 
开发者ID:straeger,项目名称:shell.me,代码行数:32,代码来源:CommandFactory.cs

示例5: ApiInstance

 //    codebase/idl/idl.somename.dll contains generated API wrapper for somename
 //    codebase/api/api.somename.dll contains actual user-written code for API
 private ApiInstance(string name, string codepath)
 {
     path = Path.Combine(Path.Combine(codepath, "idl"), "idl." + name + ".dll");
     //	todo: for runtime code replacement, use appdomains and refcount active requests
     assy = Assembly.LoadFile(path);
     if (assy == null)
     {
         throw new FileNotFoundException("Could not find assembly: " + path);
     }
     Console.WriteLine("Loaded {0} from {1}", name, path);
     foreach (Type t in assy.GetExportedTypes())
     {
         Console.WriteLine("Examining type {0}", t.Name);
         if ((t.Name == name || t.Name == "idl." + name) && typeof(WrapperBase).IsAssignableFrom(t))
         {
             Console.WriteLine("Found type {0}", t.Name);
             wrapper = (WrapperBase)assy.CreateInstance(t.FullName);
             wrapper.CodePath = codepath;
             wrapper.Initialize();
             break;
         }
     }
     if (wrapper == null)
     {
         throw new FileNotFoundException("Could not instantiate wrapper type: " + path);
     }
     api_counter.Count();
 }
开发者ID:jwatte,项目名称:mono-app-server,代码行数:30,代码来源:ApiInstance.cs

示例6: _runLoad

 public override void _runLoad(string nUrl)
 {
     UrlParser urlParser_ = new UrlParser(nUrl);
     string assemblyPath_ = urlParser_._returnResult();
     AssemblyName assemblyName_ = AssemblyName.GetAssemblyName(assemblyPath_);
     AppDomain appDomain_ = AppDomain.CurrentDomain;
     Assembly[] assemblies_ = appDomain_.GetAssemblies();
     foreach (Assembly i in assemblies_)
     {
         if (string.Compare(i.FullName, assemblyName_.FullName) == 0)
         {
             mAssembly = i;
         }
     }
     if (null == mAssembly)
     {
         mAssembly = Assembly.LoadFrom(assemblyPath_);
         string namespace_ = assemblyName_.Name;
         string pluginClass_ = namespace_ + ".Plugin";
         IPlugin plugin_ = mAssembly.CreateInstance(pluginClass_) as IPlugin;
         if (null != plugin_)
         {
             plugin_._startupPlugin();
         }
     }
     base._runLoad(nUrl);
 }
开发者ID:zyouhua,项目名称:nvwa,代码行数:27,代码来源:AssemblyUfl.cs

示例7: CreateDriverInstance

 private static IDriver CreateDriverInstance(string assemblyName, Assembly assembly)
 {
     IDriver driver = null;
     if (assembly != null) {
         driver = assembly.CreateInstance(assemblyName + ".Driver") as IDriver;
     }
     return driver;
 }
开发者ID:pzaps,项目名称:CrossGFX,代码行数:8,代码来源:DriverLoader.cs

示例8: ItemBuilder

        public ItemBuilder(Assembly asm, string namespce, string klass)
        {
            string full_name = String.Format ("{0}.{1}", namespce, klass);
            item = asm.CreateInstance (full_name) as Item;

            if (item == null)
                throw new ApplicationException (String.Format
                    ("Unable to create instance of {0}", full_name));
        }
开发者ID:manicolosi,项目名称:questar,代码行数:9,代码来源:ItemBuilder.cs

示例9: Method

 static void Method(Assembly plugin)
 {
     //This cast fails because the two MyPlugin types are incompatible.
     //The MyPlugin type which appears in the code is resolved by the JIT
     //to the load context, but the Plugin.MyPlugin type which is created
     //using reflection (Assembly.CreateInstance) is resolved to the
     //load-from context.  The exception message (as of .NET 3.5) discloses
     //the problem right away.
     MyPlugin myPlugin2 = (MyPlugin)plugin.CreateInstance("Plugin.MyPlugin");
 }
开发者ID:Helen1987,项目名称:edu,代码行数:10,代码来源:LoadContexts.cs

示例10: Migrator

 public Migrator(Assembly assembly, IMigrationFormatter formatter)
 {
     this.repository = new MigrationRepository(formatter);
     this.migrations = new List<Migration>();
     foreach (var t in assembly.GetTypes()) {
         if (t != typeof(Migration) && typeof(Migration).IsAssignableFrom(t)) {
             var m = (Migration)assembly.CreateInstance(t.ToString());
             migrations.Add(m);
         }
     }
 }
开发者ID:iescarro,项目名称:rooko,代码行数:11,代码来源:Migrator.cs

示例11: GetPluginActivator

        IPluginActivator GetPluginActivator(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (!typeof(IPluginActivator).IsAssignableFrom(type)) continue;

                return assembly.CreateInstance(type.FullName) as IPluginActivator;
            }
            // TODO: 例外を投げたい
            return null;
        }
开发者ID:willcraftia,项目名称:WindowsGame,代码行数:11,代码来源:PluginManager.cs

示例12: CreateController

        /// <summary>
        /// Creates a Controller based on the route values from the URL.
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        public Controller CreateController(Assembly assembly, string controllerName)
        {
            controllerName = controllerName.ToLower() + "controller";
            var nameSpace = assembly.GetName().Name;

            var type = assembly.GetTypes().Where(t => t.FullName.ToLower().Contains(controllerName)).FirstOrDefault();

            Controller controller = (Controller)assembly.CreateInstance(type.FullName);

            return controller;
        }
开发者ID:tacoman667,项目名称:pseudo_mvc,代码行数:16,代码来源:ControllerFactory.cs

示例13: CreateDelegate

        public static Delegate CreateDelegate(Assembly dynamicAssembly)
        {
            var dynamicObject = dynamicAssembly.CreateInstance(DYNAMIC_CLASS_NAME);
            if (dynamicObject == null)
                throw new TypeLoadException(DYNAMIC_CLASS_NAME);

            var dynamicMethod = dynamicObject.GetType().GetMethod(DYNAMIC_METHOD_NAME);
            if (dynamicMethod == null)
                throw new MissingMethodException(DYNAMIC_METHOD_NAME);

            return GetDelegate(dynamicObject, dynamicMethod);
        }
开发者ID:toschkin,项目名称:MIO,代码行数:12,代码来源:DynamicAssembly.cs

示例14: RegisterAssemblyParselets

        protected void RegisterAssemblyParselets(Assembly asm)
        {
            ID900Parselet parselet;

            foreach (Type t in asm.GetTypes())
            {
                if (t.GetInterface(typeof(ID900Parselet).Name) != null)
                {
                    parselet = (asm.CreateInstance(t.FullName, true) as ID900Parselet);
                    this.RegisterParselet(parselet);
                }
            }
        }
开发者ID:neeraj9,项目名称:d900cdr-decoder,代码行数:13,代码来源:D900ParseletProvider.cs

示例15: AfxComponentResolver

        /// <summary>
        /// Initializes a new instance of a component resolver
        /// based on the implementation located in a component
        /// package's designer assembly.
        /// </summary>
        /// <param name="asm"></param>
        /// <param name="type"></param>
        public AfxComponentResolver(Assembly asm, Type type)
        {
            var identityAttribute = GetIdentityAttribute(type);
            if (identityAttribute != null)
            {
                this.Id = new Guid(identityAttribute.Id);
                this.Name = identityAttribute.Name;
            }

            // Create an instance of the resolver interface
            // implementation and assign it to the internal
            // reference for future use:
            _instance = asm.CreateInstance(type.FullName) as IAfxComponentResolver;
        }
开发者ID:recurry,项目名称:Angelfish,代码行数:21,代码来源:AfxComponentResolver.cs


注:本文中的System.Reflection.Assembly.CreateInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。