Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Is there a way to accomplish this task. I've been trying to find a way on my own but It seems impossible to me. Thing is I want to bind class property's accessors using DataRowExtensions.Field and DataRowExtensions.SetField methods AT RUNTIME. I created my custom DataRow. It has de capability to make typed DataTable (DataTable<t> where T : DataRow<t>. Where T is a class).
C#
public class DataTable<T> :TypedTableBase<T> where T : DataRow<T>{
    
}
public class DataRow<T> : DataRow where T : class {
    
}

Inherited classes from this DataRow must to declare properties empty getters and setters. DataTable<t> has the capability to built him-self based on property types and property attributes defined and also written by me. Of course I could define properties like this.
C#
public class CustomDataRow : DataRow<CustomDataRow > {
    [PrimaryKey]
    public int Id { 
        get { 
            return DataRowExtensions.Field<int>(this,"Id");
        }
        set { 
            DataRowExtensions.SetField<int>(this,"Id",value);
        }
    }
}

But it would be comfortable and productive to build this at runtime.
Can anyone help me??
Posted
Updated 15-Oct-15 10:13am
v2
Comments
Patrice T 15-Oct-15 15:18pm    
Can anyone help me??
Yes, probably Google
Pedro Luis Gil Mora 15-Oct-15 15:29pm    
Believe me, I drained my browser on trying
Patrice T 15-Oct-15 15:31pm    
At least give an example of the problem and what you try to do.
Use Improve question
Pedro Luis Gil Mora 15-Oct-15 17:34pm    
I thought you were pleasent to improve the question
Richard Deeming 15-Oct-15 15:24pm    
Your question is unclear. What are you trying to do?

1 solution

"Dynamic Type Using Reflection.Emit" by Abhishek Sur [^].

You can create new Objects of any Type at run-time using Reflection and the System.Reflection.Emit namespace; however this is a pain in the butt to do (not something I've had a requirement to do, so far), and computationally expensive.

Another route to explore for code-generation is T4 templates:

"Using T4 Templates to generate custom strongly-typed code in Visual Studio" by Inaki Ayucar [^].
 
Share this answer
 
Comments
Pedro Luis Gil Mora 16-Oct-15 8:28am    
It's nice advice, but it requires a lot study the far I see. I won't accept the solution 'til I see results. But I like th idea.
BillWoodruff 16-Oct-15 9:04am    
Hi Pedro, you will see "results" when you make them ... happen :)

cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900