简体   繁体   中英

How create button using reflection?

I want create any instance from GAC dlls , but don't know how load ?

Assembly asemb = Assembly.LoadFile(dllName);
try
{
    obj = Activator.CreateInstance(asemb.GetType(type));
}
catch (Exception)
{
    return null;
}

I try this code but it's not work

Works for me:

var assembly = Assembly.Load("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
var type     = assembly.GetType("System.Windows.Forms.Button");
var button   = Activator.CreateInstance(type);

To get full assembly name from .dll file:

var assemblyFileName = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Windows.Forms.dll";
var assembly         = Assembly.LoadFile(assemblyFileName);
var assemblyName     = assembly.GetName().ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM