private static void Chapter8CodeBlock11() { ReflectionExample reflectionExample = new ReflectionExample(); Type reflectionExampleType = typeof(ReflectionExample); reflectionExampleType.GetField("_privateField", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(reflectionExample, "My New Value"); Debug.Print(string.Format("Private Field Value: {0}", reflectionExample.PrivateField)); }
private static void Chapter8CodeBlock14() { ReflectionExample reflectionExample = new ReflectionExample(); Type reflectionExampleType = typeof(ReflectionExample); double returnValue = (double)reflectionExampleType.InvokeMember("Multiply", BindingFlags.InvokeMethod, null, reflectionExample, new object[] { 4, 5 }); Debug.Print(string.Format("Return Value: {0}", returnValue)); }
private static void Chapter8CodeBlock13() { ReflectionExample reflectionExample = new ReflectionExample(); Type reflectionExampleType = typeof(ReflectionExample); MethodInfo methodInfo = reflectionExampleType.GetMethod("Multiply"); double returnValue = (double)methodInfo.Invoke(reflectionExample, new object[] { 4, 5 }); Debug.Print(string.Format("Return Value: {0}", returnValue)); }
public bool GetPerson(int personId) { //Open the connection to the database. SqlConnection cn = new SqlConnection("Server=(local);Database=Reflection;Trusted_Connection=True;"); cn.Open(); //Retrieve the record. SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM Person WHERE PersonId = {0}", personId), cn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return(ReflectionExample.LoadClassFromSQLDataReader(this, dr)); }
private static void Chapter8CodeBlock12() { ReflectionExample reflectionExample = new ReflectionExample(); Type reflectionExampleType = typeof(ReflectionExample); PropertyInfo[] fields = reflectionExampleType.GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); foreach (PropertyInfo field in fields) { object fieldValue = field.GetValue(reflectionExample); Debug.WriteLine(string.Format("Field Name: {0}, Value: {1}", field.Name, fieldValue.ToString())); } }