Is it possible to call Activator.CreateInstance on a type in an assembly that is not statically referenced?

StackOverflow https://stackoverflow.com/questions/14961341

문제

Is is possible to user Activator.CreateInstance() to instantiate a type given the Type.FullName and Assembly Name even though the assembly is not referenced by the executing assembly?

도움이 되었습니까?

해결책

Yes, first you have to load the assembly.

Suppose you have a "plugins" folder to look into:

            foreach (FileInfo f in new DirectoryInfo("c:\\plugins").GetFiles("*.dll"))
            {
                System.Reflection.Assembly.LoadFrom(f.FullName);
            }

Now assemblies are loaded and you can create the type using Activator.CreateInstance.

다른 팁

Yup. That is the beauty of Reflection. You can load the assembly by knowing it's name and then you can create a type that is declared within that assembly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top