问个问题,我想反射出一个DLL 文件中的接口,但是我的DLL文件没有被程序引用进来,指定路径获得
Assembly getDll = Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll");
Type[] typ = getDll.GetTypes();
Type type = Type.GetType(typ[0].ToString());
Type[] typi= type.GetInterfaces();
现在最大的问题是获取不了类的类型Type type = Type.GetType(typ[0].ToString());这句老是报错,未初始化
能帮我解决下吗,谢谢了
Assembly getDll = Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll");
Type[] typ = getDll.GetTypes();
Type type = Type.GetType(typ[0].ToString());
Type[] typi= type.GetInterfaces();
现在最大的问题是获取不了类的类型Type type = Type.GetType(typ[0].ToString());这句老是报错,未初始化
能帮我解决下吗,谢谢了
Type[] typ = getDll.GetTypes();
if(typ.Length > 0)
{
Type type = Type.GetType(typ[0].AssemblyQualifiedName);
Type[] typi= type.GetInterfaces();
}
如果分不够我还会在加的。会另开贴加分的。谢谢了
高手出现啊。谢谢了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// This is a Console program, when build, no required reference winform dll manual, but can show form as well.
Assembly asm = null;
try
{
asm = Assembly.LoadWithPartialName("System.Windows.Forms");
}
catch (Exception ex)
{
Console.WriteLine("Error load assembly: \r\n" + ex.ToString());
return;
} if (asm == null)
{
Console.WriteLine("No assembly loaded!");
return;
} Type formType = asm.GetType("System.Windows.Forms.Form");
if (formType == null)
{
Console.WriteLine("Cannot get form type.");
return;
} object form1 = Activator.CreateInstance(formType);
if (form1 == null)
{
Console.WriteLine("Cannot create form instance.");
return;
} MethodInfo showFormMethodEntry = formType.GetMethod("ShowDialog", new Type[0]);
if (showFormMethodEntry == null)
{
Console.WriteLine("Cannot get Form.ShowDialog method entry.");
return;
} showFormMethodEntry.Invoke(form1, new object[0]); Console.WriteLine("Demo Successed!");
}
}
}
Type type = Type.GetType(typ[0].ToString());
Type[] typi= type.GetInterfaces();
==================
如果typ这个Array的Length是0或者typ为null,那么使用typ[0].ToString()就会抛出异常了。
---------------------------
想问下这样做和Assembly getDll = Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll");
的区别是什么,现在我对这两个都比较模糊,都是添加引用吗,还是。。