English 中文(简体)
在整个项目中封装各类特性的通用/方便方式
原标题:A generic/easy way to encapsulate properties of classes over a whole project

Im 正在设计一个双形应用程序, 需要能够将不同类型属性的值输出到序列/ 数字端口 。

我在想,在运行时,这个工程中的所有班级是否都有一个简单的循环方式,然后向用户显示一组选择的集合(也许通过使用属性?)及其相应的字符串值,这样他们就可以输出这些值的任何组合。

所以,对于(简化的)例子来说, 说我有两个班级:

public class ClassA
{
  public double Price{get;set;}
  public int Units{get;set}
  //other properties that I don t want visible to the user
}

public class ClassB
{
  public string Name{get;set;}
  public string Description{get;set;}
  //other properties not visible to the user
}

我将有许多这类班级在运行时即刻进行(它们不一定相互连接/参照/相关)。

例如:

public ClassA Cars = new ClassA();
public ClassB Models = new ClassB();
public ClassA PCs = new ClassA();
//set properties of these instances
//etc.. more of these

我要收集所有这些实例,并向用户展示相应的字段:

Cars.Price = "1000";
Cars.Units = "3";
Models.Name = "BMW";
Models.Description = "Luxury Car";
PCs.Price = "3000";
PCs.Units = "20";

Note that the values will be changing at runtime, and this will need to be updated when sending out data. I was hoping someone could point me in the right direction here.

编辑 编辑 编辑 编辑 编辑 编辑

看来这也许不可行

是否有一种方法我可以存储一个对象列表, 这些对象指向我的属性/ 字段, 这样我就可以检索它的价值? 当然假设这些类实例将只创建一次, 我可以在我的输出类中注册分类属性/ 字段。 然后输出类可以复制到这个列表中, 获取它指向的对象的值, 然后发送所需的数据?

例如:

class OutputData
{
  Dictionary<string,object> OutputDataList = new Dictionary<string,object>();

  public void RegisterData(string displayName, ref object dataField)
  {
    OutputDataList.Add(displayName,dataField);
  }

  //iterate over OutputDataList and get the value of the property/field it points to
}
最佳回答

如果这些天体都存在到应用程序的存续期间, 你只需要跟踪所有天体, 你可以在每类的构造器里做这些, 比如:

public class Tracker
{
  public static List<object> AllObjects = new List<object>();
}

public class ClassA
{
   public ClassA()
   {
      Tracker.AllObjects.Add(this);
   }
   ...
}

这样您只需要修改您想要跟踪的类, 而实例将会在总体列表中被跟踪。 对于要跟踪的字段的问题, 您可以使用反射, 或者您可以在所有对象中存储更有趣的内容, 也许一个 Func< List< string> & gt; 返回所有感兴趣的值 。

然后增加一行如下:

      Tracker.AllObjects.Add(() => 
          { 
            yield return "Name:" + this.Name; 
            yield return "Description:" + this.Description;
          });

然后,你可以做:

var ouputLines = Tracker.AllObjects.SelectMany(o => o());
问题回答

看起来这些是 something 的字段:

public ClassA Cars = new ClassA();
public ClassB Models = new ClassB();
public ClassA PCs = new ClassA();

因此,让我们明确一点:

public class Something {
    public ClassA Cars = new ClassA();
    public ClassB Models = new ClassB();
    public ClassA PCs = new ClassA();
}

所以假设这些田地在某事中, 快速的反省会找到它们。到目前为止,如此之好。

您需要理解的是,字段的名称(例如“汽车”)与变量实例无关,它与包含该字段的类别实例有关,在此情况下,有些是 something

要理解为什么,想象一下这个代码:

var a = new Something();
var b = new Something();
b.PCs = a.Cars;

现在我们有一个类别A的变量, 存储在 a. Cars b.PCs 中。 您要使用哪个标签?

你需要有一本关于这些事物的字典,或者限制它们的名称。

public Dictionary<String, Object> myStuff = new Dictionary<String, Object> { { "Cars", new ClassA() }, { "Models", new ClassB() }, { "PCs", new ClassA() } };

// then, to get the field names and values

var someObject = myStuff["PCs"];
foreach (var fi in someObject.GetType().GetFields()) {
    var fieldName = fi.Name;
    var fieldValue = fi.GetValue(someObject);
    // Do something here
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签