Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi i have two class files. one is base another is Append the datas to base.
 
class1.cs
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
 
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 4-Dec-12 2:36am
sencsk1.2K
Comments
Programm3r at 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 at 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 at 5-Dec-12 0:38am
   
Yup - agreed
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
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);
                try
                {
                    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);
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
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