Domanda

Ho un assembly. In questo assembly ho una classe e un'interfaccia. Devo caricare questo assembly in fase di runtime e voglio creare un oggetto della classe e anche usare l'interfaccia.

Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);

Questo è il mio codice. Come potrebbe essere migliorato?

È stato utile?

Soluzione

Se l'assembly si trova in GAC o bin, utilizzare il nome dell'assembly alla fine del nome del tipo anziché Assembly.Load () .

object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));

Altri suggerimenti

Dovresti usare il metodo dinamico con per migliorare. è più veloce della riflessione ..

Ecco un codice di esempio per la creazione di oggetti usando il metodo dinamico ..

public class ObjectCreateMethod
{
    delegate object MethodInvoker();
    MethodInvoker methodHandler = null;

    public ObjectCreateMethod(Type type)
    {
        CreateMethod(type.GetConstructor(Type.EmptyTypes));
    }

    public ObjectCreateMethod(ConstructorInfo target)
    {
        CreateMethod(target);
    }

    void CreateMethod(ConstructorInfo target)
    {
        DynamicMethod dynamic = new DynamicMethod(string.Empty,
                    typeof(object),
                    new Type[0],
                    target.DeclaringType);
        ILGenerator il = dynamic.GetILGenerator();
        il.DeclareLocal(target.DeclaringType);
        il.Emit(OpCodes.Newobj, target);
        il.Emit(OpCodes.Stloc_0);
        il.Emit(OpCodes.Ldloc_0);
        il.Emit(OpCodes.Ret);

        methodHandler = (MethodInvoker)dynamic.CreateDelegate(typeof(MethodInvoker));
    }

    public object CreateInstance()
    {
        return methodHandler();
    }
}

//Use Above class for Object Creation.
ObjectCreateMethod inv = new ObjectCreateMethod(type); //Specify Type
Object obj= inv.CreateInstance();

Questo metodo richiede solo 1/10 del tempo necessario ad Activator.

Guarda http://www.ozcandegirmenci.com/ postale / 2008/02 / Crea-oggetto-casi-veloce-che-Reflection.aspx

Scopri http://www.youtube.com/watch?v=x- KK7bmo1AM Per modificare il suo codice per caricare più assembly utilizzare

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assemblyName = args.Name.Split(',').First();
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace." + assemblyName + ".dll"))
            {
                byte[] assemblyData = new byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        }
Nel tuo metodo principale metti
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Assicurati di aggiungere i tuoi assiemi al tuo progetto e modificare la proprietà dell'azione di compilazione in " Embedded Resource " ;.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top