Can you create a class dynamically with reflection?

S

Shawn

Hi;
i just started research reflection and i'm wondering if i have an empty
class file can i use reflection to add member variables and attributes
dynamically and then instantiate the class?
What i would like to be able to do is start with and empty class, then
depending on the data provided to me by a config file, add the member
variables and attributes to the class temporarily. when the app is shutdown
all changes would be gone.

can anyone point me to any articles or a book that might go over something
like this?

Thanks for any help.
 
A

Arne Vajhøj

Shawn said:
i just started research reflection and i'm wondering if i have an empty
class file can i use reflection to add member variables and attributes
dynamically and then instantiate the class?
What i would like to be able to do is start with and empty class, then
depending on the data provided to me by a config file, add the member
variables and attributes to the class temporarily. when the app is shutdown
all changes would be gone.

I don't think you can add to an existing class using reflection.

But you can generate a new class that extends an existing class
using the code generation capabilities of .NET !

Arne
 
M

Marc Gravell

Would it not be simpler to simply store the name/value pairs in a
dictionary? You can do lots of things, either with System.ComponentModel
(for runtime extensible types) or Reflection.Emit (for new dynamic
types) - but neither is trivial...

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi;
i just started research reflection and i'm wondering if i have an empty
class file can i use reflection to add member variables and attributes
dynamically and then instantiate the class?

AFAIK Reflection let you to inspect a given type, not modify it.

You can generate code though, take a look at CodeDOM.

But I would strongly advise agains it, I would explore any other
possibility before starting to generate code dynamically
 
B

Ben Voigt [C++ MVP]

Ignacio said:
AFAIK Reflection let you to inspect a given type, not modify it.

Reflection.Emit allows you to create new types and dynamic methods.

Reflection also allows you to modify static and instance member data,
including private if you have the right permission set.
You can generate code though, take a look at CodeDOM.

But I would strongly advise agains it, I would explore any other
possibility before starting to generate code dynamically

Agreed.
 
A

Aneesh Pulukkul[MCSD.Net]

Reflection.Emit allows you to create new types and dynamic methods.

Reflection also allows you to modify static and instance member data,
including private if you have the right permission set.





Agreed.

Does it allow adding properties as well?
 
M

Marc Gravell

Does it allow adding properties as well?- Hide quoted text -

You can add properties to *new* types; you cannot do much with an
existing type.

What exactly do you need to do? System.ComponentModel offers various
options* for runtime-properties against classes, suitable for things
like data-binding - but not usable by things like LINQ.

*=ICustomTypeDescriptor, TypeDescriptionProvider and TypeConverter

Marc
 
M

Mark Dykun

Shawn,

Really what you are looking at doing is not he best use of reflection.emit.
Since to use the new type you would need reflection to access its members it
does not make sense. If you are just creating a type to be a container
create a property bag class that you can store your values in and add/remove
them dynamically. What is the greater problem here, and why would you need a
type that is defined temporary. If you really want to go down the emit road
you can create a base class and derive from it adding your new
functionality. That way you can cast back for common functionality and
helpers while using the other reflection metadata to accomplish what you
are trying to do.

Mark
 
I

Ignacio Machin ( .NET/ C# MVP )

Reflection.Emit allows you to create new types and dynamic methods.

Sorry, I forgot about that, but note that Emit use IL (think assembly)
so it's not a simple task by any means.
 
S

Supun Bula

Hi Arne. I saw your reply, and that's why I thought on putting this question for you. You said that,
it is possible to generate a new class that extends an existing class using the code generation capabilities of .NET !

My question is if we create such a class dynamically, will it automatically integrated to the existing project, or can we integrated it to the existing project dynamically. (Manually of course you can do this)

Supun
 
S

Supun

Hi Arne. I saw your reply, and that's why I thought on putting this question for you. You said that,
it is possible to generate a new class that extends an existing class using the code generation capabilities of .NET !

My question is if we create such a class dynamically, will it automatically integrated to the existing project, or can we integrated it to the existing project dynamically. (Manually of course you can do this)
 
A

Arne Vajhøj

Supun said:
Hi Arne. I saw your reply, and that's why I thought on putting this question for you. You said that,
it is possible to generate a new class that extends an existing class using the code generation capabilities of .NET !

My question is if we create such a class dynamically, will it automatically integrated to the existing project, or can we integrated it to the existing project dynamically. (Manually of course you can do this)

A project is a .cs -> .exe/.dll concept. With dynamic code generation
you compile source code in memory to MSIL in memory - it is not really
related to any project.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top