C# 如何在运行时从dll引用静态类
我正在从C# 如何在运行时从dll引用静态类,c#,xslt,dll,xsltc,C#,Xslt,Dll,Xsltc,我正在从A.xslt使用xsltc.exe创建A.dll。 然后在我的项目中引用A.dll,并执行转换: XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(typeof(A)); // A is a public static class from A.dll xslt.Transform(RootPath + "A.xml", RootPath + "A.txt"); 但是我如何在运行时引用A.dll并进行转换
A.xslt
使用xsltc.exe
创建A.dll
。
然后在我的项目中引用A.dll
,并执行转换:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(typeof(A)); // A is a public static class from A.dll
xslt.Transform(RootPath + "A.xml", RootPath + "A.txt");
但是我如何在运行时引用
A.dll
并进行转换呢?如果我理解正确,您希望在运行时同时生成和引用dll。好消息是,您可以在运行时使用assembly.LoadFrom
加载程序集
下面的内容取自,这种技术称为反射
Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\A.dll");
// Obtain a reference to a method known to exist in assembly.
var aTypes = SampleAssembly.GetTypes();
MethodInfo Method = aTypes[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
// Type = System.String
// Position = 0
// Optional=False
foreach (ParameterInfo Param in Params)
{
Console.WriteLine("Param=" + Param.Name.ToString());
Console.WriteLine(" Type=" + Param.ParameterType.ToString());
Console.WriteLine(" Position=" + Param.Position.ToString());
Console.WriteLine(" Optional=" + Param.IsOptional.ToString());
}
我只想从dll中得到我的类
A
,你的代码有Method1
,我不知道应该替换什么,因为类A
中没有方法。谢谢,我忘了提到我想在运行时生成和引用DLL我更新了一点代码-在var-aTypes=samplessembly.GetTypes()上设置一个断点;MethodInfo方法=类型[0]。GetMethod(“方法1”);并使用VisualStudio中的watch功能检查aTypes对象。这将列出您的课程。aTypes[0]只是引用程序集中的第一个类。