我有一个解决方案,其中包含了两个工程,
一个为:DigitalCQ
另一个为:TestApplication
其中在工程DigitalCQ中有一个类的命名空间为:DigitalCQ.Window.Forms.FmApplicationMain
我在工程TestApplication中添加了对工程DigitalCQ的引用
然后在工程TesApplication的Test类中写了一句
Type type = Type.GetType("DigitalCQ.Window.Forms.FmApplicationMain",true,true);
运行后程序总是报错:
未处理的“System.TypeLoadException”类型的异常出现在 TestApplication.exe 中。其他信息: 未能从程序集 TestApplication, Version=1.0.1893.27006, Culture=neutral, PublicKeyToken=null 中加载类型 DigitalCQ.Window.Forms.FmApplicationMain。
一个为:DigitalCQ
另一个为:TestApplication
其中在工程DigitalCQ中有一个类的命名空间为:DigitalCQ.Window.Forms.FmApplicationMain
我在工程TestApplication中添加了对工程DigitalCQ的引用
然后在工程TesApplication的Test类中写了一句
Type type = Type.GetType("DigitalCQ.Window.Forms.FmApplicationMain",true,true);
运行后程序总是报错:
未处理的“System.TypeLoadException”类型的异常出现在 TestApplication.exe 中。其他信息: 未能从程序集 TestApplication, Version=1.0.1893.27006, Culture=neutral, PublicKeyToken=null 中加载类型 DigitalCQ.Window.Forms.FmApplicationMain。
程序总是在其自身的程序集中去寻找指定的类,
而不到其引用的程序集中寻找,
是不是不应该用这种方法?
还是应该添加什么语句?
才能解决这一问题呢?
Type type = Type.GetType("DigitalCQ.Window.Forms.FmApplicationMain.Class1",true,true);
Type t = Assembly.LoadFrom("DigitalCQ.dll").GetType("DigitalCQ.Window.Forms.FmApplicationMain",true,true);
你的类名称没写,只写了命名空间,所以出错
AppDomain currentDomain = AppDomain.CurrentDomain;//Make an array for the list of assemblies.
Assembly[] assems = currentDomain.GetAssemblies();Type type = null;
for (int j = 0; j < assems.Length ; j++)
{
Type[] types = assems[j].GetTypes();
for (int k = 0; k < types.Length; k++)
{
if ( types[k].Name.Equals(inputBeanName) || (types[k].FullName.Equals(inputBeanName)))
{
type = types[k];
break;
}
}
if ( type != null)
{
break;
}
}
strng assembly= frm.GetType().Assembly.FullName;
Type type = Type.GetType(assembly);
然后看一下 string assembly的值和你的exe的名字去掉扩展名部分 你就明白了
我的想法就是:
在知道一个对象的类型后,(当然该类型是用字符串来表示,如:“DigitalCQ.Window.Forms.FmApplicationMain”)
如何在程序中获取该类型的Type.
谢谢xixigongzhu(夕夕宝贝)和 linuxyf(率人)
光知道类型名字是不能取到Type对象的,必须知道这个类型所在的程序集。如果类型所在的程序集为mscorlib或者当前程序集,那么这种情况下就不需要指定程序集了
我的想法就是:
在知道一个对象的类型名后,(当然该类型是用字符串来表示,如:“DigitalCQ.Window.Forms.FmApplicationMain”)
如何在程序中获取该类型的Type.
如果是这样的话,介意你用我提供的方法,从当前域的所有程序集中查找。
//Create current domain
AppDomain currentDomain = AppDomain.CurrentDomain;//Make an array for the list of assemblies.
Assembly[] assems = currentDomain.GetAssemblies();Type type = null;
for (int j = 0; j < assems.Length ; j++)
{
Type[] types = assems[j].GetTypes();
for (int k = 0; k < types.Length; k++)
{
if ( types[k].Name.Equals(inputBeanName) || (types[k].FullName.Equals(inputBeanName)))
{
type = types[k];
break;
}
}
if ( type != null)
{
break;
}
}