简体   繁体   English

C#遍历类成员以查找特定条件

[英]C# traversing class members to look for a specific condition

I have a C# class that gets loaded with a database row (orm)..this object has all the data in it..i need a way to traverse class level members to look for specific condition? 我有一个C#类,该类加载了数据库行(orm)。此对象中包含所有数据。 the following is the stop-gap I have and There ought to be a better solution.. Is reflection the answer and how? 以下是我的权宜之计,应该有一个更好的解决方案。 Thanks! 谢谢!

public string findCURRRATEValue()
{
    if (!FUNDNO1.Equals("999") &&
        FUNDTYPE1.Equals("F"))
    {
        return "CURRRATE1" + "#" + CURRRATE1;
    }
    else if (!FUNDNO2.Equals("999") &&
        FUNDTYPE2.Equals("F"))
    {
        return "CURRRATE2" + "#" + CURRRATE2;
    }


------------
-------------

    else if (!FUNDNO59.Equals("999") &&
        FUNDTYPE59.Equals("F"))
    {
        return "CURRRATE59" + "#" + CURRRATE59;
    }
    else if (!FUNDNO60.Equals("999") &&
        FUNDTYPE60.Equals("F"))
    {
        return "CURRRATE60" + "#" + CURRRATE60;
    }
    else
    {
        return "CURRRATEX";
    }
} 

Speaking in reflction terms, this is what you can do: 用反射的话来说,这是您可以做的:

var type = this.GetType();
var fundNoProperties = type.GetProperties()
    .Where(p => p.Name.StartsWith("FUNDNO"));
foreach (var info in fundNoProperties)
{
    string orderNumber = info.Name.Replace("FUNDNO", "");
    var fundTypeInfo = type.GetProperty("FUNDTYPE" + orderNumber);
    if (info.GetValue(this, null).ToString() == "999" &&
        fundTypeInfo.GetValue(this, null).ToString() == "F")
    {
        var currRate = type.GetProperty("CURRATE" + orderNumber);
        return currRate.Name + currRate.GetValue(this, null).ToString();
    }
}

Of course, I have to agree with people commenting this is not how you design your databases. 当然,我必须同意人们的评论, 这不是您设计数据库的方式。 However, data mining tables might look like this (100+ columns, anyone?) or perhaps old, legacy systems tables (over which we have little control). 但是, 数据挖掘表可能看起来像这样(超过100列,有人吗?)或旧的,遗留的系统表 (我们对此无法控制)。 Dirty tricks are not such terrible solution then. 肮脏的把戏不是那么可怕的解决方案。

As others have said it is a problem with the way the database is designed. 正如其他人所说,数据库的设计方式存在问题。 The best solution would be to create a separate table which holds the current rate for each fund (per @Paulo Santos). 最好的解决方案是创建一个单独的表,其中包含每个基金的当前汇率(按@Paulo Santos)。 However, if you are stuck with this solution (like many of us are), you could use reflection, but reflection is rather slow (see solution by @Achim). 但是,如果您坚持使用此解决方案(就像我们中的许多人一样),则可以使用反射,但是反射相当慢(请参阅@Achim的解决方案)。 You could also put each variable into a Dictionary and access the rate via a Dictionary. 您也可以将每个变量放入“字典”中,并通过“字典”访问费率。 This way you don't have to duplicate logic, but if you need to add a field its a matter of adding it to the Dictionary. 这样,您不必重复逻辑,但是如果您需要添加字段,只需将其添加到字典中即可。

Yet another way would be to take the DDD approach and isolate DTOs from entities/value objects. 还有一种方法是采用DDD方法,将DTO与实体/值对象隔离。 Keep those objects and use them as if they are a DTO rather than a business object. 保留这些对象并将其当作DTO而非业务对象使用。 Then create a Fund object with a Rate property and map the DTO object to the business object. 然后,创建一个具有Rate属性的Fund对象,并将DTO对象映射到业务对象。 Ultimately, this would be the beginning of transitioning to a better data structure-- starting at the C# level. 最终,这将是过渡到更好的数据结构的开始-从C#级别开始。

Yes, you should be able to solve this via Reflection. 是的,您应该可以通过反射解决此问题。 Pseudocode out of my head which should show the idea, but will probably not compile out of the box: 伪代码应该在我脑海中显示出来,但可能不会立即编译出来:

foreach (PropertyInfo prop in this.GetType().GetProperties())
{
    object val = prop.GetValue(this,new object[0]);
    if (...your condition ...) return "xyz" + value;
}
return "last else case";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM