Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        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()));
            }
        }
Exemplo n.º 7
0
        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()));
            }
        }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        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));
        }