(Forum FAQ) How do I dynamically set properties and fields for a class?

Source: Internet
Author: User
Tags foreach reflection reset tostring
Dynamic
Someone just asked this, the code is very simple, the most basic application, direct paste code

Using System;

Namespace Test
{
/**////<summary>
Summary description of the CLASS1.
</summary>
Class Class1
{
/**////<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
TODO: Add code here to start the application
//
Myfieldclass dv=new Myfieldclass ();
System.Collections.Hashtable ht1=new System.Collections.Hashtable ();
Ht1. ADD ("Fielda", "A");
Ht1. ADD ("Fieldc", "C");
SetField1 (HT1,DV); If the field in the class matches the key in the Hashtable, then reset it.
SetField2 (HT1,DV)//If the field in the key matching class in Hashtable is reset, the effect is equal to SetField1
Console.WriteLine (DV. Fielda);//a
Console.WriteLine (DV. FIELDB);//bb
Console.WriteLine (DV. FIELDC);//c
System.Collections.Hashtable ht2=new System.Collections.Hashtable ();
Ht2. ADD ("Propertyb", "B");
Ht2. ADD ("Propertyc", "C");
SetProperty1 (HT2,DV); If the property in the class matches the key in the Hashtable, reset it.
SetProperty2 (HT2,DV); If the property in the key matching class in Hashtable is reset, the effect is equal to SetProperty1
Console.WriteLine (DV. Fielda);//a
Console.WriteLine (DV. FIELDB);//b
Console.WriteLine (DV. FIELDC);//c

}

public static void SetProperty1 (System.Collections.Hashtable ht1,myfieldclass DV)
{
foreach (System.Collections.DictionaryEntry de in ht1)
{
System.Reflection.PropertyInfo PI=DV. GetType (). GetProperty (DE. Key.tostring ());
if (pi!=null) pi. SetValue (Dv,de. Value.tostring (), NULL);
}
}

public static void SetProperty2 (System.Collections.Hashtable ht1,myfieldclass DV)
{
foreach (System.Reflection.PropertyInfo pi in DV. GetType (). GetProperties ())
{
if (ht1. Contains (pi. Name) pi. SetValue (Dv,ht1[pi. Name],null);
}
}

public static void SetField1 (System.Collections.Hashtable ht2,myfieldclass DV)
{
foreach (System.Collections.DictionaryEntry de in ht2)
{
System.Reflection.FieldInfo FI=DV. GetType (). GetField (DE. Key.tostring ());
if (fi!=null) fi. SetValue (Dv,de. Value.tostring ());
}
}

public static void SetField2 (System.Collections.Hashtable ht2,myfieldclass DV)
{
foreach (System.Reflection.FieldInfo fi in DV. GetType (). GetFields ())
{
if (ht2. Contains (FI. Name), FI. SetValue (Dv,ht2[fi. Name]);
}
}
}

public class Myfieldclass
{
public string fielda= "AA";
public string fieldb= "BB";
public string fieldc= "CC";

public string Propertya
{
Get
{
return Fielda;
}
Set
{
Fielda=value;
}
}

public string Propertyb
{
Get
{
return fieldb;
}
Set
{
Fieldb=value;
}
}

public string PROPERTYC
{
Get
{
return FIELDC;
}
Set
{
Fieldc=value;
}
}
}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.