Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi i have two class files. one is base another is Append the datas to base.

class1.cs
C#
public  string FirstName
       {
           get { return GetPropertyValue<string>("FirstName"); }
           set { SetPropertyValue("FirstName", value); }
       }
       public string LastName
       {
           get { return GetPropertyValue<string>("LastName"); }
           set { SetPropertyValue("LastName", value); }
       }


class2.cs

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



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
Comments
Programm3r 4-Dec-12 8:46am    
Hi - please have a look at the following threads:

http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
http://stackoverflow.com/questions/4657311/reflection-get-property-name
Sergey Alexandrovich Kryukov 4-Dec-12 16:23pm    
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...
--SA
Programm3r 5-Dec-12 0:38am    
Yup - agreed
Sergey Alexandrovich Kryukov 4-Dec-12 16:18pm    
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.
--SA

1 solution

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

C#
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);
                try
                {
                    ReflectionObject.GetProperty(pName).SetValue(_synMed, Convert.ToString(valueVar), null);
                }
                catch (Exception e)
                {
                }

            }

        }

    }

}


Sample use:
C#
synMed syn = new synMed();
test2 t = new test2();
t.FirstName = "Help";
t.LastName = "IdontKnow";
t.SynMedFill(ref syn);
 
Share this answer
 
v2

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