-1

Visual Studio 2010 でアプリケーションをプログラミングしていますが、C# を使用してライブラリを参照に追加したいので、使用中のライブラリとビジネス クラスのファイルに呼び出しを追加します。

public void addreference()
{

//needed code
}

public addInvocation()
{


//write in the file of business class
}

マウスを使用して参照を選択するようなものですが、C#を使用してそれを実行したいのですが、 どうすればよいですか?


重要な解決策 解決策 を使用しようとしましたが、問題が見つかりました。ライブラリのクラスを正常にインスタンス化しましたが、それらのメソッドを使用できません

最初に Interface1 というインターフェイスを作成し、次に Classe1 というクラスを作成してから、.dll を生成しました。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
  public   interface Interface1
    {

         int add();
         int sub();
    }


  public class Class1 : Interface1
  {
      Class1()
      {

      }


      #region Interface1 Members

      public int add()
      {
          return 10;
      }

      public int sub()
      {
          return -10;
      }

      #endregion
  }
}

class1 コードをインスタンス化するために関連付けられた

 string relative = "ClassLibrary1.dll";
 string absolute = Path.GetFullPath(relative);

Assembly assembly = Assembly.LoadFile(absolute);
System.Type assemblytype = assembly.GetType("ClassLibrary1.Class1");

object a = assembly.CreateInstance("ClassLibrary1.Class1", false,           BindingFlags.CreateInstance, null,null,null, null);

今、私はメソッドを追加したいのですが、どうすればそれを行うことができますか

4

2 に答える 2

1

リフレクションは明らかなオプションのようです。皮切りに

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

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

        ...

それから:

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

……とか、そんなこと。コンパイルされませんが、まだジストを取得します。

于 2013-04-25T10:28:44.243 に答える
1
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;
}
于 2013-05-09T08:10:35.150 に答える