简体   繁体   English

如何在C#中遍历通用对象属性

[英]How to traversal Generic object properties in c#

The problem is that I often encounter a scene that needs to traverse the instance object's property as another object method incoming parameters when the method calls. 问题是我经常遇到一个场景,该场景需要在实例方法的调用时作为另一个对象方法的传入参数遍历实例对象的属性。 Like follows: 如下所示:

//Step 1
var criteria =new Criteria();
SqlCommand cmd =SqlCommandManager.GetDataCommand("SearchOrderList");
//Step 2
//The define of the SetParameterValue method is :
//public void SetParameterValue(string paramName, object val);
cmd.SetParameterValue("@SellerID",criteria.SellerID );
cmd.SetParameterValue("@DateFrom", criteria.SentDateFrom);
cmd.SetParameterValue("@DateTo", criteria.SentDateTo);
cmd.SetParameterValue("@StartRowIndex", criteria.PagingInfo.StartRowIndex);
cmd.SetParameterValue("@PageSize", criteria.PagingInfo.PageSize);
cmd.SetParameterValue("@SortField", criteria.PagingInfo.SortField);
cmd.SetParameterValue("@SortType", criteria.PagingInfo.SortType);
//cmd.SetPa........
//Step 3
cmd.ExecuteEntityList<Model>();

How I make a flexible and scalable method to simplify step 2. I have a thought is that make a Generic Method to deal this: 我如何制作一种灵活且可扩展的方法来简化第2步。我想到了一种通用方法来解决此问题:

public static void SetParameterValues<T>(this DataCommand cmd,T _object)
    {
        if (Object.ReferenceEquals(cmd,null))
            throw new ArgumentException("The Parameter cmd must be not null", "cmd");
        if (!object.ReferenceEquals(_object, null))
        {
            //to do ...
        }

    }

but I'm confused what I should Proceed to deal with next step(I have made an extension Method). 但是我很困惑下一步应该做什么(我做了一个扩展方法)。 So could you guys help me check out this problem? 你们可以帮我解决这个问题吗?

Instead of making one method generic and then have do somehow differentiate between the different types, create overloaded versions of the method 而不是使一种方法通用,然后以某种方式区分不同类型,而是创建该方法的重载版本

public static void SetParameterValue(this DataCommand cmd, string parameter, int value)
public static void SetParameterValue(this DataCommand cmd, string parameter, string value)
public static void SetParameterValue(this DataCommand cmd, string parameter, DateTime value)
...

Eg 例如

public static void SetParameterValue(this DataCommand cmd, string parameter, int value)
{
    if (Object.ReferenceEquals(cmd,null))
        throw new ArgumentException("The Parameter cmd must be not null", "cmd");
    cmd.Parameters.Add(parameter, SqlDbType.Int).Value = value;
}

But if you want to infer the prameter name, you would have to use another approach. 但是,如果要推断参数名称,则必须使用另一种方法。 To be able to access the property name, you need an expression tree: 为了能够访问属性名称,您需要一个表达式树:

public static void SetParameterValue<T>(this DataCommand cmd, Expression<Func<T>> expr)
{
    var member = expr.Body as MemberExpression;
    if (member == null)
        throw new ArgumentException("We need a member expression of type obj.PropertyName");

    var prop = member.Member as PropertyInfo;
    if (prop == null)
        throw new ArgumentException("We need a property, not a field");

    string name = prop.Name;
    T value = expr.Compile()();

    cmd.Parameters.AddWithValue("@" + name, value);
}

You must then call it using a lambda expression 然后,您必须使用lambda表达式进行调用

cmd.SetParameterValue(() => criteria.SellerID); 

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

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