Created
July 8, 2017 20:06
-
-
Save harshityadav95/59b0a2e54f7c7f61f3f0c5c99d85bee4 to your computer and use it in GitHub Desktop.
Code Reflection in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
namespace After004 | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
// without reflection | |
////var dog = new Dog { NumberOfLegs = 4 }; | |
////Console.WriteLine("A dog has {0} legs", dog.NumberOfLegs); | |
// with reflection | |
object dog = Activator.CreateInstance(typeof (Dog)); | |
PropertyInfo[] properties = typeof (Dog).GetProperties(); | |
PropertyInfo numberOfLegsProperty1 = properties[0]; | |
// or | |
PropertyInfo numberOfLegsProperty2 = null; | |
foreach (PropertyInfo propertyInfo in properties) | |
{ | |
if (propertyInfo.Name.Equals("NumberOfLegs", StringComparison.InvariantCulture)) | |
{ | |
numberOfLegsProperty2 = propertyInfo; | |
} | |
} | |
numberOfLegsProperty1.SetValue(dog, 3, null); | |
Console.WriteLine(numberOfLegsProperty2.GetValue(dog, null)); | |
// use reflection to invoke different constructors | |
var defaultConstructor = typeof (Dog).GetConstructor(new Type[0]); | |
var legConstructor = typeof (Dog).GetConstructor(new [] {typeof (int)}); | |
var defaultDog = (Dog)defaultConstructor.Invoke(null); | |
Console.WriteLine(defaultDog.NumberOfLegs); | |
var legDog = (Dog) legConstructor.Invoke(new object[] {5}); | |
Console.WriteLine(legDog.NumberOfLegs); | |
} | |
} | |
internal class Dog | |
{ | |
public int NumberOfLegs { get; set; } | |
public Dog() | |
{ | |
NumberOfLegs = 4; | |
} | |
public Dog(int legs) | |
{ | |
NumberOfLegs = legs; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment