5

I have a below class

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
}

I have a query string like "my location_Locations/new york_City/city street_Address/200_MaxRecord/20_PageSize"

Above string contains the exact property names of the class SearchModel. Now I want to do something like below:-

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];
}
2
  • I would think this would open up some security concerns. If you are allowing the (untrusted internet) caller to control which variables get bound. I could for example, call /20_City/abc_PageSize/abc_SomePropertyYouDidNotPlanOnExposing/ Commented Jul 17, 2014 at 10:29
  • @ppittle: In my case, I do not care if anyone puts any non-existing property in the url, and also i am exposing all properties in the URL, so not an issue for me. But thanx for the concern. Commented Jul 17, 2014 at 10:37

1 Answer 1

4

add following codes to your class

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;
            } 
        }
    }
}

and your modified codes

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];
}

OR


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),
}

For case insensitive:

model.GetType().GetProperty(value[1], BindingFlags.IgnoreCase |  BindingFlags.Public | BindingFlags.Instance )
Sign up to request clarification or add additional context in comments.

8 Comments

Is there a way to make the property name lookup case insensitive?
@AliRızaAdıyahşi: One more thing, can we also check the datatype of the property and make the necessary type conversion?
@AliRızaAdıyahşi: Thanks, I have modified your answer and added checks for proper datatype conversion as described in above link( which you mentioned).
@JitendraPancholi, thanks, but your edit is rejected by other users, I edited again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.