473,395 Members | 1,969 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,395 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 3839
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.