簡體   English   中英

.Net套利運行時類實例化和方法調用

[英].Net arbitratry runtime class instantion and method calling

我正在尋找一種方法來進行任意的類實例化以及屬性分配,並可能在.Net(最好是C#)中進行方法調用。 由於隨意性太寬泛,所以讓我告訴你我的追求。

假設我有一個包含以下內容的DLL(objects.dll):

public class Person
{
    // Field 
    public string name;

    // Constructor that takes no arguments. 
    public Person()
    {
        name = "unknown";
    }

    // Constructor that takes one argument. 
    public Person(string nm)
    {
        name = nm;
    }

    // Method 
    public void SetName(string newName)
    {
        name = newName;
    }

}

public class Table
{
    // Field 
    public int width;
    public int lenth;
    public int height;

    // Constructor that takes no arguments. 
    public Table()
    {
        width = 0;
        length = 0;
        height = 0
    }

    // Constructor that takes three arguments. 
    public Table(int w, int l, int h)
    {
        width = w;
        length = l;
        height = h;
    }

    // Method 
    public void SetWLH(int w, int l, int h)
    {
        width = w;
        length = l;
        height = h;
    }
}

public class Printer
{
    public Printer(){}

    public void printAPerson(Person p)
    {
        //do stuff with p
    }

    public void printATable(Table t)
    {
        // do stuff with t
    }
}

我希望能夠實例化以上任何一個類,並在運行時從其他程序中以最通用的方式設置屬性值和調用方法。 例如。 可以說我有一個名為myprog.exe的程序,我希望能夠執行以下操作

myprog.exe objects.dll人員名稱testname打印機printAPerson其中:

  • objects.dll是包含所有必需類的dll

  • 人是我首先要實例化的名稱是它的屬性

  • testname是我將分配給該屬性的值

  • 打印機是我將用於打印的課程

  • printAPerson是我需要使用指定對象作為參數調用的Printer類中的方法。

如您所見,在最適合我的使用場景的情況下,在編譯時都不知道/不會知道對象和類,因此我希望盡可能地不進行廣播。 如果那不可能,我會盡我所能。

我已經看到了, 如何使用反射來調用方法並傳遞在編譯時類型未知的參數? ,據我所知,它只是做我想要的減去鑄模位的事情,否則我可能會誤會。

非常感謝!

您對可執行輸入格式是否靈活? 如果是這樣,您可以通過約定來做您想做的事情。 我將使用這樣的JSON結構進行此操作:

{
   Command : "",
   Arguments : {
      Argument1 : 0,
      Argument2 : {  }, // can be another complex object
      Argument3 : [] // an empty array maybe ...
     }
}

其中Command類似於“ ClassName.MethodName”,參數將是一個JSON對象,每個對象屬性代表您的方法參數。

在可執行文件中,您必須使用庫(例如http://www.newtonsoft.com/json )解析此JSON,並使用反射反序列化每個JSON對象參數並調用您的方法。 如果您無法使它正常工作,請告訴我,我將嘗試舉一個例子(如果今天晚上我有時間,因為我現在正在工作)。

對於您的情況,您只想在打印機上打印“人”類型的對象? 您可以執行以下命令:

{
  Command : "Printer.PrintAPerson",
  Arguments : {
   p : { name : 'george' }
  }
}

如果要依賴標准協議,請檢查JSON-RPC協議: http : //json-rpc.org/wiki/specification

除了使用Reflection之外,您還可以使用dynamic 但這需要更改Printer類和其他類。 並且您將失去智能感知並編譯時間檢查。

public class Printer
{
    public Printer() { }

    public void printAPerson(dynamic p)
    {
        //do stuff with p
        Console.WriteLine("Person name: " + p.name);
    }

    public void printATable(dynamic t)
    {
        // do stuff with t
        Console.WriteLine("printATable(Table p) is called");
    }
}

public class TestDynamic
{
    public static void Test()
    {
        // To get the type by name, 
        // the full type name (namespace + type name) is needed
        Type personType = Type.GetType("StackOverflowCodes.Person");
        object personObj = Activator.CreateInstance(personType);

        // Implicit cast to dynamic
        dynamic person = personObj;
        person.SetName("Alan Turing");

        Type printerType = Type.GetType("StackOverflowCodes.Printer");
        object printerObj = Activator.CreateInstance(printerType);

        dynamic printer = printerObj;
        printer.printAPerson(personObj);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM