mercredi 13 décembre 2017

Way to embed/reference another DLL assembly while invoking a method using C# Reflection

Is there anyway to invoke methods using C# Reflection with embedded/reference dlls?

For example consider the following senario. I have a assembly call User.dll, which have the class as bellow

namespace User
{
    public class UserInfo
    {
        public static string Name = "Username";
    }
}

Using the above dll as reference, I can able to compile the following code and access UserInfo.Name variable.

using User;
using System.Windows.Forms;

public class Test
{
    public Test()
    {
        MessageBox.Show("Name : " + UserInfo.Name);
    }
}

Consider the above code is in another dll called Test.dll assembly. Using Assembly.LoadFile("Test.dll") and C# Reflection, when I try to invoke the Constructor, getting File not found runtime error.

Error

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'DynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
   at Test..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)





Aucun commentaire:

Enregistrer un commentaire