无法从引用的程序集创建类的类型。 [英] Not able to create Type of class from referenced assembly.

查看:56
本文介绍了无法从引用的程序集创建类的类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨开发者,



我有一个程序集并调用了AssemblyRefTest.dll,这在我的项目中被称为AssemblyRefClient。 AssemlyRefTest程序集具有名为AssemblyClass.cs的类,我想使用如下所示的反射创建该类的对象。

Hi Developers,

I have an assembly and called AssemblyRefTest.dll and this is refereed in my project called AssemblyRefClient. AssemlyRefTest assembly is having class named as AssemblyClass.cs and i want to created object of that class using reflection like below.

var classType = Type.GetType("AssemblyRefTest.AssemblyClass");



但执行此操作后,classType变量为null。



我可以直接在我的项目中访问该类,如下所示。




but the classType variable is null after this is executed.

and i can access the class directly in my project like below.

AssemblyClass classType = new AssemblyClass();





我已经在我的项目中放置了相同的类AssemblyClass.cs这次我可以使用反射创建该类的对象它将正常工作。


中间的
使用它来加载组装只是为了尝试。





and i have placed the same class AssemblyClass.cs in my project this time i can created object of that class using reflection it will work fine.

in middle have used this to load assemly just for try.

AppDomain appdomain = AppDomain.CurrentDomain;
            appdomain.Load("AssemblyRefTest");





然后它也不起作用。



请任何人帮我解决这个问题。



谢谢&问候,

V_R



then also it is not working.

Pleas any one help me to solve this issue.

Thanks & Regards,
V_R

推荐答案

////////get class name from database or any other source
  string ClassName = "AssemblyRefTest.AssemblyClass";
  if (ClassName != string.Empty)
  {     
     //////////check which dll has the class
          if (ClassName.StartsWith("AssemblyRefTest"))
           {
               //////////////load classes from dll
               System.Reflection.Assembly assembly =                    System.Reflection.Assembly.LoadFile(Application.StartupPath + "\\AssemblyRefTest.dll");
               //////////get type of class
               Type tp = assembly.GetType(ClassName);
             ///////////////use Activator.CreateInstance(tp)  For creating objects of the Class
             }
             else  if (ClassName.StartsWith("anotherdll.dll"))
                      {
                         ////////  use nested else if ,for loading from multiple dll's
                        }
    }


这篇关于无法从引用的程序集创建类的类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆