Jak załadować Assembly w czasie wykonywania i utworzyć instancję klasy?

Mam zebranie. W tym montażu mam klasę i interfejs. Muszę załadować ten assembly w czasie wykonywania i chcę utworzyć obiekt klasy, a także chcę korzystać z interfejsu.

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);
To jest mój kod. Jak można to poprawić?
Author: daniele3004, 2009-11-26

3 answers

Jeśli twój zespół jest w GAC lub bin, użyj nazwy zespołu na końcu nazwy typu zamiast Assembly.Load().

object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));
 20
Author: Mehdi Golchin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-26 14:11:40

Powinieneś użyć dynamicznej metody Z do poprawy. to szybsze niż odbicie..

Oto przykładowy kod do tworzenia obiektu metodą dynamiczną..

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();

Ta metoda zajmuje tylko 1/10 czasu potrzebnego aktywatorowi.

Zobacz http://www.ozcandegirmenci.com/post/2008/02/Create-object-instances-Faster-than-Reflection.aspx

 13
Author: Sasikumar D.R.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-22 13:40:21

Zobacz http://www.youtube.com/watch?v=x-KK7bmo1AM Aby zmodyfikować jego kod, aby załadować wiele zestawów, użyj

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);
            }
        }
W swojej głównej metodzie umieścić
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Pamiętaj, aby dodać swoje zespoły do projektu i zmienić właściwość build action na "Embedded Resource".
 2
Author: reggaeguitar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-24 22:24:21