Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to AddProperty to a C# class at runtime
Message
From
26/07/2007 00:27:44
 
 
To
25/07/2007 15:25:10
General information
Forum:
ASP.NET
Category:
Web Services
Miscellaneous
Thread ID:
01243387
Message ID:
01243670
Views:
25
Hi Gary

Thank you for your reply. I think from yours and other's comments I may have got my terms wrong. Maybe what I want is to add fields at runtime.

I am an absolute newbie so anything you say will be appreciated.

The dictionary idea sounds like it would work but is beyond me without a sample that addresses my example. For instance the line
dictionary<string, object> 
is that to be entered as is or should I substitute some string for the word string? You get my drift. This is all so new for me.

Maybe if I explain a bit more what I want to achieve.

I have to return this object from a web service every time with the following fields (vfp properties):
A
ResponseObject
    SessionID
    ErrorCode
    ErrorText
I also have a few other classes like this:
B
MemberDetails
    Name
    DOB
    Phone
    Email
C
CreditCardInfo
    CCName
    CCNumber
    CCExpiry
D
Address
    ADDLine1
    AddLine2
    City
    Postcode
E
BankInfo
    Accountname
    AcNumber
    BSB
    BANK
and a few other classes. like those above

Object A will always be returned. But depending on the Function called I may need to return

A ValidateSession()
A+B UpdateUserDetails() GetMemberDetailsByMemberid()
A+C UpdateCCDetails() GetCCDetails()
A+D Get or SetMemberAddress()
A+E GET or SetMemberBankDetails()
A+B+C GetMemberDetailsWithCC()
A+B+D GetMemberDetailsWithAddress()
A+B+C+D GetAllDetails()

etc and combinations of these as needed. So my idea was to create these as separate classes and then depending on the function called add the appropriate classes (with their fields) to the ResponseObject, fill the values and send it back.

I just don;t want to create so many classes as A, A+B, A+C, A+B+C etc. and combinations of that.

So can you come up with some idea how this can be achieved in C#?

Thanks for your kind help so far.

Bernard



>Hi Bernard
>
>>If I have a similar class in C# and want to add a single property say - "newEmail" at runtime
>>What is the command to add it to the class below?
>
>First, you cannot add a property to a C# class at runtime. In fact, in C#, it isn't the same at all. A property effectively "wraps" a private class member. You cannot add a class member at run-time. Moreover, why would you want to?
>
>What you did in Fox (ie AddProperty) was something that it could do and people used this capability - I used to do it myself. However, when I look back, why did I do it? What made my class designs so bad that I felt compelled to add properties at run-time? I think it is probably time to re-think this approach and consign it to whence it came :)
>
>I haven't looked at the dynamic language runtime for the CLR and it is possible that you may be able to do this sort of thing with a dynamic CLR language. In C#, you could always create a dictionary or hashtable of type "object" and add named/value pairs at runtime so, your added "property" would consist of a name (the dictionary "key") and the value. When you want to reference your "newEmail" dictionary entry, you would just cast the object value to a string. When you think about it, given that VFP isn't strongly typed, this kind of approach isn't too different to AddProperty(), albeit somewhat syntactically different.
>
>
>
>// Create class level field.
>dictionary<string, object> _runtimeProperties = new dictionary<string, object>();
>
>// Wrap it in a property.
>public dictionary<string, object> RuntimeProperties
>{
>   get { return _runtimeProperties; }
>}
>
>...
>
>// In some internal class method, you can reference the collection.
>_runtimeProperties.Add("newEmail", "bernard@someplace.com");
>
>...
>
>// External to the class, reference the collection via the property.
>classInstanceName.RuntimeProperties.Add("newEmail", "bernard@someplace.com");
>
>
>
>
>
>HTH
>
>-=Gary
>
>
>
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform