To The Point aja ya..

Reflection merupakan metode untuk mengambil informasi atau meta data dari sebuah objek pada saat runtime.

Informasi yang bisa diambil seperti : nama variable, tipe variable, nilai variable, anotasi variabel, nama method, dll.

Reflection bisa digunakan untuk validasi , testing, dll.

Berikut Contoh Reflection di C#

  Console Program yang mengambil nama variabel, nilai variabel, dan nilai anotasi MapField
  Semoga Bermanfaat..
 public static class QueryHelpers
        public static String BuildStatementQueryInsert<T>(T obj)
            Type tipeObj = obj.GetType();
            FieldInfo[] fields = tipeObj.GetFields();

            foreach (var field in fields) {

                // mengambil anotasi yang ada di setiap attibut
                foreach (var attr in field.GetCustomAttributes(true))

                    //jika anotasi bertipe MapField
                    if (attr is MapField)
                        MapField mapfield = (MapField)attr;
                        Console.WriteLine ("Anotasi  : " + mapfield.NameField );


            //Nama Variabel dan Nilai Variable
            Console.writeLine("Nama Field" + field.Name+"Isi Field" + field.GetValue(obj));


//sebagai anotasi
 class MapField : Attribute
        public string NameField{set;get;}

        public MapField(string NameField_)
            NameField = NameField_;

public class Program

        [MapField("Nama")] //anotasi
        public String nama ;
        public String alamat;
        public String usia;

        static void Main(string[] args)
            Program p = new Program();
            p.nama = "Dot Net";
            p.alamat = "Server";
            p.usia = "Unlimited";
            Console.WriteLine( QueryHelpers.BuildStatementQueryInsert(p));

No Responses Yet to “Reflection”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: