locked
How to add new Properties to an existing class at runtime independent of existing members with read-write capability in c#

    Question

  • I need to make a property for each column in a database table but we do not know the number of columns before hand. It is mainly for reuseability.

    As i do not know the number of properties i need to create or their names i need them to be created at runtime according to the database table columns. we do not know before hand which database table it will be associated with.

    the coder does not need to know the names of the properties before hand. The name of the property required will be given to us by the user of the application. he will already know the database table and the columns and their names. I am restricted from using a datatable or a hashtable or a hashset or a dictionary or a list. I need to create properties.

    Wednesday, December 15, 2010 6:37 AM

Answers

  • Hello

    Welcome to MSDN Forum

    To make it easier for developers using reflection or communicating with other components (some components could be .NET-dynamic languages such as Python or Ruby, or COM objects), the c# compiler offers you a way to mark an expression’s type as dynamic. You can also put the result of an expression into a variable and you can mark a variable’s type as dynamic. This dynamic expression/variable can then be used to invoke a member such as a field, a property/indexer, a method, delegate, and unary/binary/conversion operators. The exact operation to execute based on the actual type of the object now referenced by the dynamic expression/variable will be determined at runtime.

    I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.

    Best Regards,

    Jelly

    Wednesday, December 15, 2010 8:11 AM
  • You know .NET & MSDN are just amazing. You can do this with PropertyBuilder class in Reflection.Emit namespace.

    So, buddy you need to understand 'Reflection' as well.

    For your sake of convenience here is the link to show you how to create property dynamically:

    http://msdn.microsoft.com/en-us/library/system.reflection.emit.propertybuilder.aspx


    Santosh.
    Wednesday, December 15, 2010 12:22 PM

All replies

  • Hello

    Welcome to MSDN Forum

    To make it easier for developers using reflection or communicating with other components (some components could be .NET-dynamic languages such as Python or Ruby, or COM objects), the c# compiler offers you a way to mark an expression’s type as dynamic. You can also put the result of an expression into a variable and you can mark a variable’s type as dynamic. This dynamic expression/variable can then be used to invoke a member such as a field, a property/indexer, a method, delegate, and unary/binary/conversion operators. The exact operation to execute based on the actual type of the object now referenced by the dynamic expression/variable will be determined at runtime.

    I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.

    Best Regards,

    Jelly

    Wednesday, December 15, 2010 8:11 AM
  • You know .NET & MSDN are just amazing. You can do this with PropertyBuilder class in Reflection.Emit namespace.

    So, buddy you need to understand 'Reflection' as well.

    For your sake of convenience here is the link to show you how to create property dynamically:

    http://msdn.microsoft.com/en-us/library/system.reflection.emit.propertybuilder.aspx


    Santosh.
    Wednesday, December 15, 2010 12:22 PM
  • Hi SharpSaber,

    Welcome to MSDN forum and thanks for your posting! Hope you can learn as well as contributing to our forum!
    J
    Now I have proposed useful answers and will mark them shortly.
    If these won't do good to your question, you could “Unmark As Answer”.

    Sincerely,
    Leo Liu

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 21, 2010 2:29 AM