0

I want to create several properties on the fly inside the class' constructor and add them to the current class. The property's template is the following

public [PropertyType] [PropertyName]
{
get {return Container.Resolve();}
}

I've read a little about CodeDom and Reflection.Emit but haven't been able to figure out how to use them in this particular use case.

Any clue would be greatly appreciated. Thanks in advance

EDIT: I'm elaborating on the purpose due to the number of questions... I'm trying to implement the base class for a view model locator. The binding in xaml is achieved through referring to a property by its path, therefore it MUST be a property. The types and names of the properties to be added are known only at runtime, therefore I'm using reflection to get them. In short, I need an example of taking a piece of code and compiling it dynamically into the given class.

user759141
  • 577
  • 1
  • 6
  • 11
  • How would that call work? Did you forget to add a parameter to `Resolve()`? – svick Jun 12 '11 at 10:45
  • The Resolve() method would work without parameters. It's not the issue here... – user759141 Jun 12 '11 at 10:47
  • 1
    Why do you want to add properties to the class? To call them you'd need to use reflection. And if you're using reflection then why don't you use it to simply execute the content you want in that property getter? – CodesInChaos Jun 12 '11 at 10:50
  • I get that it's not the point of the question, but I'm curious: how do you intend `Resolve()` to work? If you want to use something like `StackTrace`, that won't work because of inlining. – svick Jun 12 '11 at 11:27
  • In fact, it has worked. That's exactly the way I implemented it – user759141 Jun 12 '11 at 11:35
  • In Debug builds, it will work. In Release build, it's likely that it won't work. – svick Jun 12 '11 at 11:38
  • It works even in Release mode. Just checked it :) – user759141 Jun 12 '11 at 11:42

4 Answers4

2

If you're under C# 4, you can use the new dynamic features for that. Use ExpandoObject or DynamicObject to add properties to your object dynamically. If you then refer to them in XAML, they will get resolved correctly.

In any case, it's not possible to modify existing class. What you can is to create a new class, which can inherit from existing class, and add the properties to that. You should be able to bind to them from XAML.

svick
  • 236,525
  • 50
  • 385
  • 514
  • do you happen to have any code sample to expan on this approach? – user759141 Jun 12 '11 at 11:22
  • Which one? Using C#4 and dynamic objects or creating new class dynamically? – svick Jun 12 '11 at 11:25
  • thanks a lot for the clue. It really does the trick. I didn't even need to create a new class as I simply made the existing one inherit from the DynamicObject. The code is much cleaner now – user759141 Jun 12 '11 at 11:34
1

alternative approach:

you could implement ICustomTypeDescriptor and provide the additional properties

link: Issue with Grid data binding

Community
  • 1
  • 1
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
0

Bind your xaml stuff to somthing like xml and change xml in the runtime

Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
-1

You can create new calss and inherit it from the current class ,the new class can have the on the fly properties and base class functionality

yes its possible take look at this Help required in adding new methods, properties into existing classes dynamically

Community
  • 1
  • 1
DeveloperX
  • 4,633
  • 17
  • 22