C# 如何使用c为程序集添加引用

C# 如何使用c为程序集添加引用,c#,project,C#,Project,我正在VisualStudio2010中编写一个应用程序,我想使用c向引用添加一个库,因此我将在业务类的文件中添加using库和调用 public void addreference() { //needed code } public addInvocation() { //write in the file of business class } 这就像使用鼠标选择添加引用,但我希望使用c来做到这一点 我怎么做 临界溶液 我试图使用解决方案,但发现了一个问题,我成功地实例化了库的类

我正在VisualStudio2010中编写一个应用程序,我想使用c向引用添加一个库,因此我将在业务类的文件中添加using库和调用

public void addreference()
{

//needed code
}

public addInvocation()
{


//write in the file of business class
}
这就像使用鼠标选择添加引用,但我希望使用c来做到这一点 我怎么做

临界溶液 我试图使用解决方案,但发现了一个问题,我成功地实例化了库的类,但我无法使用它们的方法

首先,我创建了一个名为Interface1的接口 其次,我创建了一个名为Classe1的类 然后我生成了.dll

守则:

我绑定到实例化class1 代码


现在我希望调用方法add how-can-it

反射似乎是显而易见的选择。从

foreach (FileInfo dllFile in exeLocation.GetFiles("*.dll"))
{

    Assembly assembly = Assembly.LoadFile(dllFile.FullName);

        ...
然后:

最后

IMyPlugInterface myPlugInterface = (IMyPlugInterface) Activator.CreateInstance(concretePlugIn);

…或者类似的东西。它不会编译,但会获得jist。

右键单击您的项目并选择添加引用。从中选择要添加的库。请确保包含正在使用的库的命名空间。否,我正在尝试使用c代码执行此操作。是否要在设计时引用程序集,还是要在运行时动态加载程序集?换句话说,您知道在代码运行之前需要加载哪个程序集吗?实际上不是该程序集的副本。您可以在运行时使用反射加载程序集:这实际上没有帮助。所做的只是获取有关当前正在运行的程序集的文件信息。他想运行一个未添加的。更新后,我在同一时间与其他人交谈并剪切粘贴了错误的代码!这是第二部分,从反射中获取类型以便他可以使用它们,但是他如何从尚未引用的文件加载程序集?
var a = Activator.CreateInstance(assemblytype, argtoppass);
System.Type type = a.GetType();
if (type != null)
{
  string methodName = "methodname";
  MethodInfo methodInfo = type.GetMethod(methodName);
  object resultpath = methodInfo.Invoke(a, argtoppass);
  res = (string)resultpath;
}
Type[] exportedTypes = assembly.GetExportedTypes();
foreach (Type exportedType in exportedTypes)
{
    //look at each instantiable class in the assembly
    if (!exportedType.IsClass || exportedType.IsAbstract)
    {
        continue;
    }

//get the interfaces implemented by this class
Type[] interfaces = exportedType.GetInterfaces();
foreach (Type interfaceType in interfaces)
{
    //if it implements IMyPlugInterface then we want it
    if (interfaceType == typeof(IMyPlugInterface))
    {
        concretePlugIn = exportedType;
        break;
    }
}
IMyPlugInterface myPlugInterface = (IMyPlugInterface) Activator.CreateInstance(concretePlugIn);
var a = Activator.CreateInstance(assemblytype, argtoppass);
System.Type type = a.GetType();
if (type != null)
{
  string methodName = "methodname";
  MethodInfo methodInfo = type.GetMethod(methodName);
  object resultpath = methodInfo.Invoke(a, argtoppass);
  res = (string)resultpath;
}