471,344 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

Can you create a class dynamically with reflection?

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.
Jun 27 '08 #1
8 3696
Shawn wrote:
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
Jun 27 '08 #2
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
Jun 27 '08 #3
On Apr 30, 9:11*pm, Shawn <Sh...@discussions.microsoft.comwrote:
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
Jun 27 '08 #4
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 30, 9:11 pm, Shawn <Sh...@discussions.microsoft.comwrote:
>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.
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.
Jun 27 '08 #5
On May 1, 10:58*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 30, 9:11 pm, Shawn <Sh...@discussions.microsoft.comwrote:
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.

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.
Does it allow adding properties as well?
Jun 27 '08 #6
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
Jun 27 '08 #7
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
"Shawn" <Sh***@discussions.microsoft.comwrote in message
news:09**********************************@microsof t.com...
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.
Jun 27 '08 #8
On May 1, 1:58*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 30, 9:11 pm, Shawn <Sh...@discussions.microsoft.comwrote:
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.

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.
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ray | last post: by
2 posts views Thread by macca | last post: by
7 posts views Thread by pmclinn | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.