c# - 在另一个程序集中重用一个对象..需要帮助知道在哪里看/要学习什么以获得答案

标签 c# .net

可能会被否决,但我需要确定我的问题,这样我才能知道在什么地方/在哪里寻找答案。

我在程序集 A 中定义了“设置”object1。对象是属性,但与 B、C、D 不同

我在程序集 B 中定义了“设置”object2。对象是属性,但与 A、C、D 不同

我在程序集 C 中定义了“设置”object3。对象是属性,但与 A、B、D 不同

我在程序集 D 中定义了“设置”object4。对象是属性,但与 A、B、C 不同

等....

这些“设置”对象都有一个共同的属性。属性Q。

对象 L 将始终被安装,但 A-D 将以不同的组合出现。所以我同意 A-D 引用 L,但我不希望 L 引用 A-D。

我在程序集 L 中有一个方法,由 A、B、C、D 等使用。此方法将在设置对象中查找属性 Q。

在我找到正在传递的程序集设置对象之前,我不想做一些丑陋的黑客测试/尝试/戳。可能会添加更多的程序集 E,我不想重建程序集 L 来向黑客添加另一个“设置”对象以寻找..

我似乎想使用泛型和反射,但不确定这是否正确?

So to zoom in a bit closer her is an example of the settings objects:

//ASSEMBLY A
namespace User.Purchase.Product.A
{
    class SettingsA
   {
       public string Name{get;set;}
       public string Rank{get;set;}
       public string Serial{get;set;}
       public string Active{get;set;}
   }

   class DoWorkA
   {
      var PPP = new Object L; //psuedo code
      PPP.GetSettings(typeof(SettingsA));
   }

}


//ASSEMBLY B
namespace User.Purchase.Product.B
{
    class SettingsB
   {
       public string Branch{get;set;}
       public string MOS{get;set;}
       public string Promotion{get;set;}
       public string Active{get;set;}
   }

    class DoWorkB
   {
      var PPP = new Object L; //psuedo code
      PPP.GetSettings(typeof(SettingsB));
   }
}

//ASSEMBLY SettingsC
namespace User.Purchase.Product.C
{
    class C
   {
       public string Vehicle{get;set;}
       public string Engine{get;set;}
       public string FuelType{get;set;}
       public string Active{get;set;}
   }

   class DoWorkC
   {
      var PPP = new Object L; //psuedo code
      PPP.GetSettings(typeof(SettingsC));
   }
}

Common Assembly L

//ASSEMBLY L
namespace User.Common.Library
{
    class L
   {
      public bool GetSettings(Type type) //???? What / how to pass my generic object
      {
          ....somehow using reflection dynamically build my object definition for use
          by settings service below.

          var TTT = SomeManipulation(type); //obvious psuedo code here

          var xyz = settingService.LoadConfigObject<TTT>();

          if(xyz.Q)
          {...}

         //the settingService is already in existence and I would prefer to use it
         //instead of breaking DRY and creating an implementation just for this purpose.
      }
   }
}

Again my apologies for such a cloudy question.

我只是在寻找一些方向,了解我需要学习什么才能理解这种情况......或者我可能需要修改我的体系结构,因为我把自己逼到了一个角落等等。

感谢您的耐心等待和帮助。

最佳答案

创建另一个程序集并向该程序集添加一个接口(interface),该接口(interface)指定所有这些设置对象之间的公共(public)部分。

即。像这样:

public interface ICommonSettings
{
    string Q { get; set; }
}

然后从项目 A-D 和 L 中引用这个新程序集,并在这 4 个设置对象上实现这个接口(interface)。 L 中将使用来自 A-D 的设置对象的代码可以查找该接口(interface)并确定设置对象实现了公共(public)部分,但也使用“正确的方式”访问这些属性。

在引用方面你会有这个,其中“A --> B”意味着 A 引用 B:

illustration

您也可以将接口(interface)放入 L 程序集,只要 L 不需要引用 A-D,这样您就可以减少所需程序集的数量。但是,我个人认为仅接口(interface)程序集是一种更好的方法,因为您已经提取了您所说的“第三部分 dll 可以依赖这些位”的位。

关于c# - 在另一个程序集中重用一个对象..需要帮助知道在哪里看/要学习什么以获得答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26654621/

相关文章:

c# - 字符串为 Nullable 返回 false

c# - 如何告诉一个类应该创建哪些对象?类型与对象混淆 :(

.net - cs 项目文件和 nuspec 文件的 future

c# - CheckedChanged 事件中的问题

c# - 连续音生成

c# - Java 事务 API 和 .NET System.Transactions

c# - 如何更改 WPF 用户控件的两个不同控件中的字体大小?

.net - 如何使用主干单页应用程序在 MVC4 中测试 AntiForgeryToken

c# - 为什么我的 appsettings.json 文件中的配置值不通过 Configuration.GetSection().Get<MyObject> 映射到我的自定义 POCO 对象的字段?

c# - 如何在 Xamarin.iOS 中使用数据保护?