Click here to Skip to main content
13,202,706 members (55,902 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi i have two class files. one is base another is Append the datas to base.

public  string FirstName
           get { return GetPropertyValue<string>("FirstName"); }
           set { SetPropertyValue("FirstName", value); }
       public string LastName
           get { return GetPropertyValue<string>("LastName"); }
           set { SetPropertyValue("LastName", value); }


case "FIRST NAME":
                        var propinfo = synMed.GetType().GetProperty("FirstName");
                        propinfo.SetValue(info.FirstName, value,new[] {propinfo}  );
                        //if (patientBasicInfo.Contains("First Name"))
                        //    info.FirstName = value;
                    case "LAST NAME":
                        if (patientBasicInfo.Contains("Last Name"))
                            info.LastName = value;

I have import class1 as "synMed". Now i using static way. just using switch case. But i need to make it dynamically.i dont know how to apply Reflection.The comment line i haved using now. the above code is i'm trying to implement Reflection. Please Help me..
Posted 4-Dec-12 1:36am
Programm3r 4-Dec-12 8:46am
Hi - please have a look at the following threads:
Bad idea. You hard code "FirstName", "LastName", etc., and the content of these immediate constants is not checked by the compiler. This is not maintainable way at all. You can use Reflection without finding anything by name. I can answer your questions only if you thoroughly describe your purpose.
This is exactly right idea -- not to use "magic strings" (see my comment below), but I don't really like the article. Maybe you feel the same, otherwise you would post a formal answer... I answered related questions many times, but it might need detailed article...
Programm3r 5-Dec-12 0:38am
Yup - agreed

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try the following by adding into your Class2: This would set your class1(SynMed) properties from class2 properties:

public void SynMedFill(ref synMed _synMed)
    Type ReflectionDataObject = this.GetType();
    object classDataObject = this;
    Type ReflectionObject = _synMed.GetType();
    if (ReflectionObject != null)
        foreach (PropertyInfo propertyInfo in ReflectionObject.GetProperties())
            string pName = propertyInfo.Name;
            var pinfo = ReflectionDataObject.GetProperty(pName);
            if (pinfo != null)
                var valueVar = ReflectionDataObject.GetProperty(pName).GetValue(classDataObject, null);
                    ReflectionObject.GetProperty(pName).SetValue(_synMed, Convert.ToString(valueVar), null);
                catch (Exception e)

Sample use:
synMed syn = new synMed();
test2 t = new test2();
t.FirstName = "Help";
t.LastName = "IdontKnow";
t.SynMedFill(ref syn);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100