There is a C#.net library project whose DLL name is customer.dll
. It has a class name customer
with a function name show()
. I want to call this function from another project but don't want to add the reference to the caller project. Can this be done? Is there any C#.net class that can achieve this?
Asked
Active
Viewed 2,025 times
0

gorkem
- 731
- 1
- 10
- 17

Vaibhav Srivastava
- 39
- 1
- 11
-
1you can check how to use reflection in c#. assembly load and access the Method create a instances and access the method – umasankar Jul 05 '17 at 06:22
-
2Possible duplicate of [How to use reflection to call method by name](https://stackoverflow.com/questions/3110280/how-to-use-reflection-to-call-method-by-name) – garfbradaz Jul 05 '17 at 06:40
1 Answers
8
Yes, you can load assemblies dynamically using Assembly.LoadFile
Assembly.LoadFile("c:\\somefolder\\Path\\To\\Code.dll");
You are then going to need to use Reflection to get the methodinfo for the function you want to invoke or use the Dynamic keyword to invoke it.
var externalDll = Assembly.LoadFile("c:\\somefolder\\Customer.dll");
var externalTypeByName = externalDll.GetType("CustomerClassNamespace.Customer");
// If you don't know the full type name, use linq
var externalType = externalDll.ExportedTypes.FirstOrDefault(x => x.Name == "Customer");
//if the method is not static create an instance.
//using dynamic
dynamic dynamicInstance = Activator.CreateInstance(externalType);
var dynamicResult = dynamicInstance.show();
// or using reflection
var reflectionInstance = Activator.CreateInstance(externalType);
var methodInfo = theType.GetMethod("show");
var result = methodInfo.Invoke(reflectionInstance, null);
// Again you could also use LINQ to get the method
var methodLINQ = externalType.GetMethods().FirstOrDefault(x => x.Name == "show");
var resultLINQ = methodLINQ.Invoke(reflectionInstance, null);

Alexander Higgins
- 6,765
- 1
- 23
- 41
-
Thank you.But What should i write to achieve this .Please Give me some link then i can easily follow – Vaibhav Srivastava Jul 05 '17 at 06:31
-
Confused. I just showed you how to load the DLL and invoke the method. You simply need to change the path to the DLL and TypeName parameters. If you need further documentation Google `Assembly.LoadFile` and check out the reference documents on MSDN. – Alexander Higgins Jul 05 '17 at 06:36
-
-
-
What error? You can use LINQ to find the type `var externalType = externalDll.ExportedTypes.FirstOrDefault(x => x.Name == "Customer");` Same for the method – Alexander Higgins Jul 05 '17 at 07:02
-
-
I am getting Exception->Absolute path information is required. Assembly.LoadFile() – Vaibhav Srivastava Jul 05 '17 at 07:27
-
The put an absolute path in there. "c:\\somefolder\customer.dll" – Alexander Higgins Jul 05 '17 at 07:33
-
But the problem is it can be change and my file extension is .exe not .dll . – Vaibhav Srivastava Jul 05 '17 at 07:37
-
-
Some exe's are .net modules and can be loaded the same way. Your question said you had `customer.dll` Many times exe's have dll's they reference. If not you'll have to ask for a DLL. – Alexander Higgins Jul 05 '17 at 07:40
-