473,473 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to dynamically add propery in a Class in C#?

3 New Member
Hi All,

I need to create property in a class dynamically.
Property names will come from database and also the total number of properies can be changed according to database value.
I have tried with CodeDome,but unable to solve.
Please suggest me.
Dec 23 '08 #1
3 5625
Plater
7,872 Recognized Expert Expert
I think you might be able to do this with Reflection, but I've not done it myself.
Assuming that you want this to be built at run time, you will never be able to write in your code myobject.SomeDataBaseProperty.

It would be very easy to have it work like myobject.GetProperty("SomePropertyName")
Dec 23 '08 #2
mldisibio
190 Recognized Expert New Member
Have a look at the System.Reflection.Emit namespace, particularly the PropertyBuilder class.

However, I can firmly suggest that this is not the preferred solution from a design standpoint. Are you building a business application? If so, you need to take maintainability, readability and flexibility into account. Using Reflection to dynamically emit code to solve a business problem gets very low marks on the first two (maintainability, readability).

Another approach you might consider is the concept of a "PropertyBag." There is an actual class with this name related to Web classes, but you don't need to use that class to implement the concept. Basically, you associate a Dictionary with your class. The Key is the PropertyName and the Value is the PropertyValue. You can have a flexible number of properties with names created at runtime. You can then write one or two functions or indexers which allows you to retrieve the property value by name after looking it up in the Dictionary.

A third option would be to implement the "PropertyBag" concept using an Xml Document or Node. You could pull the properties and values directly from the database. Each element would be a property. You could add attributes describing the property type and legitimate values (similar to an xml schema document). And again, write a method which traverses the document and looks up properties based on name.
Dec 23 '08 #3
With .Net 4.0 you can create anonymous classes and create propertyName = value in the declaration of the anonymous class. LINQ to SQL does this.
Nov 16 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: pablo | last post by:
Dear NGers, I would like to change the alt-text with the changing of the image during a mouseover action. Can document.images.altView be changed dynamically? TIA, pablo
10
by: Ron Vecchi | last post by:
I am creating a custom web control that uses an enum for a property {Remote,InLine}. If the property is set to inline then text is read from the embedded resource and sent to the browser in the...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
2
by: Charith Jayasundara via .NET 247 | last post by:
(Type your message here) I have created some textboxes dynamically. I want to know how to use java scripts to validate them Thanks -------------------------------- From: Charith Jayasundara ...
1
by: craigkenisston | last post by:
I have a project, in which in my default.aspx page, I dinamically load a user control in codebehind, in the pageload, that manages the website : Control objPageSkin = null; HtmlForm objForm =...
2
by: Steve Franks | last post by:
In ASP.NET 2.0 you can now apparently do this: <asp:label runat="server" text="some browser" IE:text="any IE browser" IE5:text="the IE 5 browser" PIE:text="the Pocket PC browser" /> Now the...
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 =...
2
by: mfr | last post by:
Hi all, In a project I'm working on, I need to call web services methods based on information that is given to my client program only at run-time. I get the wsdl url, the name of the method to...
5
by: kmcmanus | last post by:
I have just started to write a few business classes that are largely made up of properties (getters and setters). For each setter I want to fire a changed event - each event will have a unique...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.