Click here to Skip to main content
15,911,848 members
Home / Discussions / C#
   

C#

 
GeneralRe: (OT) Re: Thread Timing Issues (Debug/Release) Pin
Dave Kreskowiak4-Feb-06 7:06
mveDave Kreskowiak4-Feb-06 7:06 
QuestionShowing a Form quickly Pin
ranzask3-Feb-06 12:20
ranzask3-Feb-06 12:20 
AnswerRe: Showing a Form quickly Pin
Dave Kreskowiak3-Feb-06 13:35
mveDave Kreskowiak3-Feb-06 13:35 
GeneralRe: Showing a Form quickly Pin
ranzask3-Feb-06 14:19
ranzask3-Feb-06 14:19 
GeneralRe: Showing a Form quickly Pin
Dave Kreskowiak3-Feb-06 15:49
mveDave Kreskowiak3-Feb-06 15:49 
Question.xsd Pin
fmardani3-Feb-06 12:02
fmardani3-Feb-06 12:02 
Question.xsd files Pin
fmardani3-Feb-06 10:36
fmardani3-Feb-06 10:36 
QuestionFun with setting subproperties through reflection Pin
tantiboh3-Feb-06 10:08
tantiboh3-Feb-06 10:08 
What I'm trying to do is set up a system that can read records from a database and build a form according to the information contained in those records.

I've largely succeeded, but I'm having difficulty setting up a way to set sub-properties.

For demonstration purposes, I will use a concrete example. Let's say I'm trying to set up a label. I want to use database records to set the label's properties. One of the properties I want to set is Font. In the case of Font, there are subproperties, i.e. Bold, Italic, Name, Size, etc. In my example, I want the font to be bold, which means I need to set the Font.Bold subproperty to True.

I've pulled the applicable data from the database into a dataset called dsMyDataSet, set up as follows:

tblTemplateControls
ControlID | ControlType
1 | System.Web.UI.WebControls.Label

tblControlProperties
PropertyID | ControlID | PropertyName | PropertyValue | ParentPropertyID
1 | 1 | Font | NULL | 0
2 | 1 | Bold | True | 1
3 | 1 | Text | Hi, Mom! | 0

Here's the code I'm using:
<br />
        //Instantiate controls. This is well-developed and works great. I include it for context.<br />
        for (int n = 0; n < this.dsMyDataSet.Tables["tblTemplateControls"].Rows.Count; n++)<br />
        {<br />
            dr = this.MyDataSet.Tables["tblTemplateControls"].Rows[n];<br />
<br />
                Type myControlType = typeof(Control).Assembly.GetType((string)dr["ControlType"]);<br />
<br />
                ConstructorInfo myControlContructor = myControlType.GetConstructor(System.Type.EmptyTypes);<br />
                Control myControl = (Control)myControlContructor.Invoke(null);<br />
                myControl.ID = Convert.ToString((int)dr["ControlID"]);<br />
<br />
                //Set the properties of the control. The works well for top-level properties like Text.<br />
                DataRow[] Properties = this.dsMyDataSet.Tables["tblControlProperties"].Select("ControlID = " + myControl.ID);<br />
<br />
                for (int j = 0; j < Properties.Length; j++)<br />
                {<br />
                    DataRow Property = Properties[j];<br />
                    if ((int)Property["ParentPropertyID"] == 0) //This property does not have a parent property.<br />
                    {<br />
                        foreach (PropertyInfo pi in myControlType.GetProperties())<br />
                        {<br />
                            if (pi.Name == Property["PropertyName"].ToString().Trim())<br />
                            {<br />
                                MethodInfo mi = pi.GetSetMethod();<br />
<br />
                                    string str = Property["PropertyValue"] is DBNull ? "" : Property["PropertyValue"].ToString().Trim();<br />
                                    if (str != "")<br />
                                    {<br />
                                        mi.Invoke(myControl, new object[1] { Convert.ChangeType(str, pi.PropertyType) });<br />
                                    }<br />
                                <br />
                                break;<br />
                            }<br />
                        }<br />
                    }<br />
<br />
                    //Here's where the trouble starts. If the property's data says it is not a top-level<br />
                    //property (i.e. ParentPropertyID != 0), then it is handled here.<br />
                    else <br />
                    {<br />
                        //Find the row representing the parent property<br />
                        DataRow[] ParentProperty = this.dsMyDataSet.Tables["tblControlProperties"].Select("ControlID = " + myControl.ID + " AND PropertyID = " + Property["ParentPropertyID"].ToString());<br />
                        //Track down the property in the parent. In our example, Font.<br />
                        foreach (PropertyInfo pi in myControlType.GetProperties())<br />
                        {<br />
                            if (pi.Name == ParentProperty[0]["PropertyName"].ToString().Trim())<br />
                            {<br />
                                //We've found the property, now we will find our subproperty.<br />
                                MethodInfo mi = pi.GetSetMethod();<br />
<br />
                                Type myPropertyType = typeof(Control).Assembly.GetType(pi.PropertyType.FullName);<br />
                                foreach (PropertyInfo pi2 in myPropertyType.GetProperties())<br />
                                {<br />
                                    if (pi2.Name == Property["PropertyName"].ToString().Trim())<br />
                                    {<br />
                                        //We've found our subproperty. If you write pi2.Name as output, you will get "Bold".<br />
                                        MethodInfo mi2 = pi2.GetSetMethod();<br />
                                            string str = Property["PropertyValue"] is DBNull ? "" : Property["PropertyValue"].ToString().Trim();<br />
                                            if (str != "")<br />
                                            {<br />
                                                //Here's the brick wall: How do I now set the value I'm aiming for? Below are a couple of lines I was playing around with.<br />
                                                //pi.SetValue(myControl, pi2.SetValue(pi, new object[1] { Convert.ChangeType(str, pi2.PropertyType) }, null), null);<br />
                                                //mi.Invoke(myControl, new object[1] { Convert.ChangeType(str, pi2.PropertyType) });<br />
                                            }<br />
                                        break;<br />
                                    }<br />
                                }<br />
                            }<br />
                        }<br />
                    }<br />
        }<br />

Frankly, I've just started playing with Reflection and Assembly, so there are some gaps in my understanding. Am I even pointing to the right spot? If not, how do I do so?

I am not above changing the structure of my database if that will provide a solution.

Thanks for your help!
QuestionLisview 2.0 Databinding Pin
machocr3-Feb-06 9:05
machocr3-Feb-06 9:05 
AnswerRe: Lisview 2.0 Databinding Pin
Ravi Bhavnani3-Feb-06 9:37
professionalRavi Bhavnani3-Feb-06 9:37 
GeneralRe: Lisview 2.0 Databinding Pin
machocr3-Feb-06 11:03
machocr3-Feb-06 11:03 
QuestionAquiring publish version number Pin
Nazadus3-Feb-06 9:05
Nazadus3-Feb-06 9:05 
QuestionComparing Data Pin
Kenneth Childs3-Feb-06 8:21
Kenneth Childs3-Feb-06 8:21 
AnswerRe: Comparing Data Pin
Pualee3-Feb-06 8:34
Pualee3-Feb-06 8:34 
GeneralRe: Comparing Data Pin
Kenneth Childs3-Feb-06 8:39
Kenneth Childs3-Feb-06 8:39 
GeneralRe: Comparing Data Pin
Pualee3-Feb-06 8:50
Pualee3-Feb-06 8:50 
Questionproblem when opening a form Pin
melanieab3-Feb-06 8:17
melanieab3-Feb-06 8:17 
AnswerRe: problem when opening a form Pin
S. Senthil Kumar4-Feb-06 20:09
S. Senthil Kumar4-Feb-06 20:09 
QuestionDatabinding with combobox as 'lookup' table Pin
Rajendra Rana3-Feb-06 8:13
Rajendra Rana3-Feb-06 8:13 
Questionhow to create registry objects of Skype (TM) Pin
Sasuko3-Feb-06 8:07
Sasuko3-Feb-06 8:07 
QuestionExcel Borders Problem Pin
FreewareFire3-Feb-06 6:26
FreewareFire3-Feb-06 6:26 
AnswerRe: Excel Borders Problem Pin
Douglas Troy3-Feb-06 6:42
Douglas Troy3-Feb-06 6:42 
QuestionRe: Excel Borders Problem Pin
FreewareFire3-Feb-06 12:51
FreewareFire3-Feb-06 12:51 
AnswerRe: Excel Borders Problem Pin
Member 42856225-Nov-08 1:49
Member 42856225-Nov-08 1:49 
Questionmouse move and click in any other window Pin
toposte3-Feb-06 5:28
toposte3-Feb-06 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.