1

I have a View Mode class.

public class VM_MyClass
{
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Date Of Birth")]
    public string DOB { get; set; }
}

I want to use its attributes like this:

VM_MyClass model = new VM_MyClass();
model[0] = "Alpha" // Name
model[1] = "11/11/14" // DOB

Is there any possibility?

8
  • 1
    Indexer (although using 0 based indexes for named properties sounds like not the best idea) Commented Sep 15, 2014 at 15:51
  • No, but why do you want this? What you're trying to achieve? Commented Sep 15, 2014 at 15:51
  • @Andre, of course it's possible... Commented Sep 15, 2014 at 15:53
  • There is indeed a way using reflection that can do something very similar to this (you have to find the PropertyInfos and the use accessors) - but as @Andre said: why? Commented Sep 15, 2014 at 15:54
  • Mohsin, why exactly you want to do this? It's a bad idea however you look at it, probably from every possible angle of looking at it.. Commented Sep 15, 2014 at 15:54

2 Answers 2

2

You can use an indexer and the reflection API like in the following example
(As other have said you must be very careful)

public class VM_MyClass
{
    private static Type ThisType = typeof(VM_MyClass);
    public string Name { get; set; }

    public DateTime DOB { get; set; }

    public object this[string propertyName]
    {
        get
        {
            return GetValueUsingReflection(propertyName);
        }
        set
        {

            SetValueUsingReflection(propertyName, value);
        }
    }

    private void SetValueUsingReflection(string propertyName, object value)
    {
        PropertyInfo pinfo = ThisType.GetProperty(propertyName);
        pinfo.SetValue(this, value, null);
    }
    private object GetValueUsingReflection(string propertyName)
    {
        PropertyInfo pinfo = ThisType.GetProperty(propertyName);
        return pinfo.GetValue(this,null);
    }
}

You can use it like this:

using System;
using System.Reflection;
namespace Example
{
  class Program
  {
    static void Main(string[] args)
    {
        VM_MyClass model = new VM_MyClass();

        model["Name"] = "My name";
        model["DOB"] = DateTime.Today;

        Console.WriteLine(model.Name);
        Console.WriteLine(model.DOB);
        //OR
        Console.WriteLine(model["Name"]);
        Console.WriteLine(model["DOB"]);

        Console.ReadLine();
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

@Georg Vovos, Thanks man for your answer, I also have solved it by this approach. As per my above comment I need to assign values of array to model attributes. So I found this way.

I am using your approach, which is more appropriate :) thanks for help.

    VM_MyClass model = new VM_MyClass();

    var ClassProperties = model.GetType().GetProperties();

    int counter = 0;
    foreach (var item in col)
    {
        Type type = model.GetType();

        System.Reflection.PropertyInfo propertyInfo = type.GetProperty(ClassProperties[counter].Name);

        propertyInfo.SetValue(model, item.InnerText);

        counter++;
    }

Comments

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.