0

I need to create properties dynamically.
My scenario is like this:
I have many classes that derive from BaseClass. BaseClass has a dictionary where inherited classes should define stuff. Those definitions are done like this.

public string Type 
{
    get 
    {
        return dictionary["type"];
    }
    set
    {
        dictionary["type"] = value;
    }
}

I will have many inherited methods like this in my derived classes, where the name of the property is almost identical to the key that references its corresponding value in the dictionary. So my idea is to add attributes in the derived classes, starting with an underscore, to distinguish them and use them to create those properties.

  1. How would I define dynamically those properties at run-time?
  2. Is it possible to define those at compile-time? Would it make sense to do so?
7
  • 2
    That look like a property to me, not a method :) Commented Nov 11, 2013 at 13:50
  • You're right... what a fail, correcting... Commented Nov 11, 2013 at 13:51
  • 2
    @E.Vaughan Properties are nothing more than methods. :) Commented Nov 11, 2013 at 13:52
  • @Erandros You can create methods dynamically, but do you really want that? Commented Nov 11, 2013 at 13:52
  • A class derived from DynamicObject seems more appropriate Commented Nov 11, 2013 at 13:53

3 Answers 3

2

Unless am missing something. You can do something like this with ExpandoObject.

dynamic obj = new ExpandoObject();
obj.Type = "123";//Create Type property dynamically
Console.WriteLine(obj.Type);//Access dynamically created Type property
Sign up to request clarification or add additional context in comments.

4 Comments

The more interesting question, perhaps, is: is it useful to the developer to do this. In many cases, having indexer access is probably more relevent
@MarcGravell I've not used this in production code, No need for this till now. But I hope this is the answer for the question How would I define dynamically those properties at run-time As you said indexer is enough though.
Don't get me wrong - it is the best of the answers here (at time of writing); if anything, my thought there is aimed at the OP. What you have here will work
@MarcGravell Yeah, maybe it's unnecesary and I'll end up explicitly defining them. Nice answer though.
0

To build those at compile-time under the hood (no C# behind it), see PostSharp (it allows you to define custom patterns)

But to "create" new functions, you may be interested in:

  • Expression.Compile to create delegates (dynamic methods, which are garbage collectible in .net 4.5) Pros: that's still C#. Cons: That does not allow creating types.

  • Reflection.Emit (in .Net framework) or Mono.Cecil (thirdparty) if you want to emit dynamic types or methods at runtime. Using this, a solution to your question would be to define your properties as abstract, and dynamically create a type (at runtime) which inherits from you abstract class defined in C#, an implements it automatically. Pros: VERY powerfull. Cons: You have to learn IL :)

Comments

0

You can create functions dynamically as follows,

Expression<Func<int, int, string>> exp = (x, y) => (x + y).ToString();
var func = exp.Compile();
Console.WriteLine(func.Invoke(2, 3));

Also you can put them in a dictionary and use them dynamically.

Expression<Func<int, int, string>> expSum = (x, y) => (x + y).ToString();
Expression<Func<int, int, string>> expMul = (x, y) => (x*y).ToString();
Expression<Func<int, int, string>> expSub = (x, y) => (x - y).ToString();

Dictionary<string, Expression<Func<int, int, string>>> myExpressions =
    new Dictionary<string, Expression<Func<int, int, string>>>();

myExpressions.Add("summation", expSum);
myExpressions.Add("multiplication", expMul);
myExpressions.Add("subtraction", expSub);

foreach (var myExpression in myExpressions)
{
    Console.WriteLine(myExpression.Value.Compile().Invoke(2, 3));
}

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.