How to reference a static class from dll at runtime

116 Views Asked by At

I'm using xsltc.exe create A.dll from A.xslt. Then refenrence A.dll in my project and do the transform:

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");

but how can I refenrece A.dll at runtime and do the transform?

1

There are 1 best solutions below

2
On BEST ANSWER

If I understand correctly, you want to both generate and reference the DLL all at run time. The good news is that you can load an assembly at run time using Assembly.LoadFrom.

The below is taken from the documentation and the technique is called Reflection.

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());
}