本文整理汇总了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;
}
示例2: DataAccessProviderFactory
static DataAccessProviderFactory()
{
string providerName = ConfigurationManager.AppSettings["DataProvider"];
string providerFactoryName = ConfigurationManager.AppSettings["DataProviderFactory"];
activeProvider = Assembly.Load(providerName);
activeDataProviderFactory = (IDataProviderFactory)activeProvider.CreateInstance(providerFactoryName);
}
示例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;
}
}
示例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>();
}
}
示例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();
}
示例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);
}
示例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;
}
示例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));
}
示例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");
}
示例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);
}
}
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
}
}
示例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;
}