简体   繁体   中英

Add a property to already created Type at runtime C# ASP.NET

I had a requirement of generating classes and its objects at runtime. Hence, looking at this article I created the same. (using )

I am storing all created types in a list.

But now the other requirement is to add properties to already created Types.

This is for the reason, if i want to use say Class A as a property Type in Class B and say Both in Class C.

I read a lot of articles on the same but have not yet come to a solution

Any Help will be appreciated.

Thanks


Actually, i am in process of developing a multitenant application like LitwareHR by Microsoft.

This will be a system where admin can make sub sites with same escalation management functionality (like MS sharepoint)

Everything is done except workflows!

For data to be stored in tables, i am storing it in XML format..

Eg:

<root tablename="UserInfo">
<column name=\"Name\">Miron</column>
<column name=\"Company\">IBM</column>
</root>"

Everything from controls on the page to events to validators to web parts gets created on runtime using XSLT.

Here, the challenge comes when i need to use expression evaluator to apply workflows to it.

Eg: If UserInfo.Name == "Miron"

Everything gets created on runtime, so have to retrieve table info as an object.

Let me know if i am not clear!

If the types exist then this gets very tricky; you can't add actual properties to an existing type, but if the code that *inspects *the values uses TypeDescriptor (which most data-binding does ) then you can add properties sort of via custom PropertyDescriptor s - either by implementing ICustomTypeDescriptor (which requires that you do something at build), or TypeDescriptionProvider .

Both are very complex, and both also demand that you have somewhere handy to put the extra data (a property-bag).

Note that in 4.0, dynamic may have some usefulness here.

If you want to avoid this, then just wrap the types in something that looks similar but with extra properties. It'll get the job done while retaining sanity.

Yes, you can use Composition as you described to do this, but classically one would use inheritence for adding functionality to an existing type.

It is difficult to answer your question without more detail about how these classes are to be used, what will be calling them and how.

I believe you will have to derive your classes from single base. Also, to be able to:

use say Class A as a property Type in Class B and say Both in Class C.

you will have to prepare class A, in case of it being a property of B; and classes A and B ready for them to be a property in Class C.

It would be helpful if you can add more information to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM