0

I want to create a property of a class, and set the name of that property using a variable value.

E.g.

string resultValue = "stack"; 
MyClass myclass = new MyClass();
myclass.resultValue = "overflow";

Now here in this case, stack is the property name and "overflow" is the value of the stack property.

Please tell how is it possible.

Thanks in advance.

1
  • Why are you trying to generate property? Commented Mar 29, 2012 at 7:04

7 Answers 7

3

You could use Reflection. For example:

string name = "stack";
string value = "overflow";
PropertyInfo pi = typeof(MyClass).GetProperty(name);
MyClass instance = new MyClass();
pi.SetValue(instance, value, null);
// at this stage instance.stack equals to "overflow"

This obviously assumes that MyClass has a public property stack with a public setter:

public class MyClass
{
    public string stack { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What the difference between instance.stack = value?
The difference is that you don't have to know the name of the variable at compile-time.
Anyway, you need to check pi != null after getting it by name.
Strange.. question was about generating property, not about accessing by name.
1

You should use Reflection for this

MyClass myclass = new MyClass();
Type type = typeof(MyClass);
PropertyInfo property = type.GetProperty("stack");
property.SetValue(myclass, "overflow", null);

Comments

0

I think that the Dynamic Source Code Generation and Compilation section on MSDN should contain the information you need.

The .NET Framework includes a mechanism called the Code Document Object Model (CodeDOM) that enables developers of programs that emit source code to generate source code in multiple programming languages at run time, based on a single model that represents the code to render.

I think that you could also get this done using Reflection.

Comments

0

Maybe you should take a look at DynamicObject. With this you can create properties at runtime by simply assigning a value. An example can also be found at the MSDN article.

Comments

0

Have at look at Mark Gravell's FastMember

It allows you to set properties in a dynamic manner.

// obj could be static or DLR 
var wrapped = ObjectAccessor.Create(obj); 
string propName = // something known only at runtime 

FastMember Blog Post

Comments

0

I can't imagine why you want to generate property on your class, but consider other ways. E.g. using Dictionary:

class MyClass
{
    Dictionary<string, object> _values = new Dictionary<string, object>();

    public object this[string name]
    {
        get
        {
            if (!_values.ContainsKey(name))
                throw new ArgumentException(name);

            return _values[name];
        }

        set
        {
            if (!_values.ContainsKey(name))
            {
                _values.Add(name, value);
                return;
            }

            _values[name] = value;
        }
    }
}

Usage:

MyClass myClass = new MyClass();
myClass["stack"] = "overflow";
Console.WriteLine(myClass["stack"]);

2 Comments

You can skip the entire if clause in your set method because _values[name] = value adds the key if not present.
@ChrisWue Thanks, forgot that!
-1

As far as I know, is this not possible. You can try using a dictionary.

2 Comments

@hamad maybe Mariusz doesn't know about Reflection, then he didn't lie! :)
@Bazzz :) then one shouldn't reply without research and proper understanding

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.