通过方法调用将自定义对象传递到DLL

本文关键字:DLL 对象 自定义 方法 调用 | 更新日期: 2023-09-27 18:00:28

所以我有一个名称空间:

namespace Main
{
  Class DoStuff
  {
    Code...
    var tableVars = new Dictionary<string, tableObject>();
    Code...
    string insertResponse = MyDLL.Insert(someString, tableVars, someString);
    Code../
  }
  public class tableObject
  {
    public string Name { get; set; }
    public string Type { get; set; }
    public string Value { get; set; }
    public string Default { get; set; }
  }
}

我有第二个名称空间:

namespace MyDLL
{
  public static string Insert(string table, Dictionary<string, tableObject> tableVars, string connection)
  {
    Code...
  }
}

在我的第一个命名空间Main中,我引用了第二个命名空间MyDLL。如何让DLL在不引用Main命名空间的情况下识别我的自定义对象(因为这会导致循环引用)?

我也尝试过使用var关键字,但在这种情况下不起作用:

namespace MyDLL
    {
      public static string Insert(string table, var tableVars, string connection)
      {
        Code...
      }
    }

通过方法调用将自定义对象传递到DLL

将所有常见类型(本例中为tableObject)移动到第三个dll,并从两个更高的程序集

中引用它

我昨天阅读了中介绍的ExpandoObject。NET 4.0,使用此对象类型,您可以在运行时动态创建对象,并将对象传递到其他命名空间。

这不是一个完美的解决方案,因为可能存在性能问题(我不敢相信Expando对象的速度和静态对象一样快)。也没有编译(或运行)时检查来确保添加了所有字段。

Dynamic/ExpandoObject还支持向对象中添加方法。

但是,这里有一些样本来源:

    void Method1()
    {

        var tableVars = new Dictionary<string, dynamic>();
        dynamic sampleObject = new ExpandoObject();
        sampleObject.Name = "Count";
        sampleObject.Type = "Int32";
        sampleObject.Value = "4";
        sampleObject.Default = "0";
        tableVars.Add("Sample", sampleObject);
        Insert("TableName", tableVars, "Connection");

    }
    string Insert(string table, Dictionary<string, dynamic> tableVars, string connection)
    {
        foreach (var v in tableVars)
        {
            Console.Write("Name is: ");
            Console.WriteLine(v.Value.Name);
        }
        return "";
    }

根据您的情况,按照@Benny在回答中指出的做法可能会更好,但我想介绍DLR的元素作为选项