簡體   English   中英

動態將對象屬性與字符串綁定

[英]Bind object properties with string dynamically

我有一堂課

public class SearchModel
{
    public string Locations { get; set; }
    public string City { get; set; }
    public string Address { get; set; } 
    public string MaxRecord { get; set; }
    public string PageSize { get; set; }
    ....
    ....
    Approx 80 properties
}

我有一個查詢字符串,例如"my location_Locations/new york_City/city street_Address/200_MaxRecord/20_PageSize"

上面的字符串包含類SearchModel的確切屬性名稱。 現在我想做以下事情:

SearchModel model = new SearchModel();
string[] Parameters = "my location_Locations/new york_City/city street_Address/200_MaxRecord/20_PageSize".Split('/');
foreach (string item in Parameters)
{
    string[] value=item.Split('_');
    model.value[1] = value[0];
}

將以下代碼添加到您的課程中

public class SearchModel
{
    public object this[string propertyName] 
    {
        get{
           Type myType = typeof(MyClass);                   
           PropertyInfo myPropInfo = myType.GetProperty(propertyName);
           return myPropInfo.GetValue(this, null);
        }
        set{
           Type myType = typeof(SearchModel);
            PropertyInfo myPropInfo = myType.GetProperty(propertyName);
            switch (myPropInfo.PropertyType.Name)
            {
                case "Int32":
                    myPropInfo.SetValue(this, Convert.ToInt32(value), null);
                    break;
                case "Int64":
                    myPropInfo.SetValue(this, Convert.ToInt64(value), null);
                    break;
                case "Boolean":
                    myPropInfo.SetValue(this, Convert.ToBoolean(value), null);
                    break;
                case "DateTime":
                    myPropInfo.SetValue(this, Convert.ToDateTime(value), null);
                    break;
                case "String":
                    myPropInfo.SetValue(this, value.ToString(), null);
                    break;
                default:
                    myPropInfo.SetValue(this, value, null);
                    break;
            } 
        }
    }
}

和您修改的代碼

SearchModel model = new SearchModel();
string[] Parameters = "my location_Locations/new york_City/city street_Address/200_MaxRecord/20_PageSize".Split('/');
foreach (string item in Parameters)
{
    string[] value= item.Split('_');
    // I changed this line
    model[value[1]] = value[0];
}

要么


SearchModel model = new SearchModel();
string[] Parameters = "my location_Locations/new york_City/city street_Address/200_MaxRecord/20_PageSize".Split('/');
foreach (string item in Parameters)
{
    string[] value= item.Split('_');

    PropertyInfo propertyInfo = model.GetType().GetProperty(value[1]);
    propertyInfo.SetValue(model, Convert.ChangeType(value[0], propertyInfo.PropertyType),
}

對於不區分大小寫的:

model.GetType().GetProperty(value[1], BindingFlags.IgnoreCase |  BindingFlags.Public | BindingFlags.Instance )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM