473,249 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 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 3827
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Allard | last post by:
Hello I'm sure this is already posted somewhere, I'm just not using the correct search words or something. Anyway, I want to store a class name in a database:...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
2
by: macca | last post by:
Hi, I am writing a GUI application. It will have a number of user defined controls( I plan to use/create a user defined control that will output alarm states that the user can also select and...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
4
by: jesper_lofgren | last post by:
Hi there, I have some webcontrols that i want to add dynamically on a page. I have stored the path / namespace in database (ex MyNameSpace.WebControls.Control1) to the class/webcontrol. Lets...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
5
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName ...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.