How to call a managed dll file in c #?

I use a scripting language, but I have a serious problem.

I need to make sure that you can call the .NET DLL in the language, but I have not found a way to do this in C #.

Does anyone know how I can load and invoke a .NET DLL? (I can't just add the link, so don't say that)

+2
source share
3 answers

Here is how I did it:

Assembly assembly = Assembly.LoadFrom(assemblyName); System.Type type = assembly.GetType(typeName); Object o = Activator.CreateInstance(type); IYourType yourObj = (o as IYourType); 

where assemblyName and typeName are strings, for example:

 string assemblyName = @"C:\foo\yourDLL.dll"; string typeName = "YourCompany.YourProject.YourClass";//a fully qualified type name 

then you can call methods on your object:

 yourObj.DoSomething(someParameter); 

Of course, what methods you can call is determined by your IYourType interface ...

+4
source

You can use Assembly.LoadFrom , from there use standard reflection to get types and methods (suppose you already do this in your scripts). An example on the MSDN page (linked) shows this:

 Assembly SampleAssembly; SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll"); // Obtain a reference to a method known to exist in assembly. MethodInfo Method = SampleAssembly.GetTypes()[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()); } 
+2
source

It looks like you need to use one of the Assembly.Load overloads ( Assembly.Load on MSDN ). After dynamically loading your assembly, you can use System.Reflection, dynamic objects and / or interfaces / base classes to access them internally.

+1
source

All Articles