예제 #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static bool IsNullableValueType(System.Reflection.TypeInfo typeInfo, out Type underlyingValueType)
        {
            if (typeInfo.IsGenericType &&
                !typeInfo.IsGenericTypeDefinition &&
                typeInfo.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                underlyingValueType = typeInfo.GetGenericArguments()[0];
                return(true);
            }

            underlyingValueType = null;
            return(false);
        }
예제 #2
0
        public void TestExpressionExtensions()
        {
            MyTestClass testClass = new MyTestClass();

            //Todo ... show how to allow to TypedReference use inside the body
            System.TypedReference member = __makeref(testClass.Test);

            System.Type type = typeof(System.Reflection.PropertyInfo);

            System.Reflection.MethodInfo setter = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.LoadSetter(() => testClass.Test.Equals(null));

            System.Console.WriteLine("setter Name: " + setter.Name);

            System.Console.WriteLine(".Test => " + testClass.Test);

            setter.Invoke(testClass, new[] { "Test" });

            if (false == (string.Compare(testClass.Test, "Test") == 0))
            {
                throw new System.Exception("Did not invoke setter");
            }

            System.Console.WriteLine(".Test => " + testClass.Test);

            testClass = new MyTestClass <int>();

            MyTestClass <int> casted = testClass as MyTestClass <int>;

            System.Console.WriteLine("<int>.Test => " + testClass.Test);

            setter.Invoke(testClass, new[] { "Test" });

            if (false == (string.Compare(testClass.Test, "Test") == 0))
            {
                throw new System.Exception("Did not invoke setter");
            }

            System.Console.WriteLine(".Test => " + testClass.Test);

            System.Console.WriteLine("<int>.Test => " + ((MyTestClass <int>)testClass).Test);

            System.Console.WriteLine("<int>.Property => " + ((MyTestClass <int>)testClass).Property);

            setter = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.LoadSetter(() => ((MyTestClass <int>)testClass).Property.Equals(null));

            System.Console.WriteLine("setter Name: " + setter.Name);

            setter.Invoke(testClass, new object[] { 1 });

            if (false == (casted.Property == 1))
            {
                throw new System.Exception("Did not invoke setter");
            }

            System.Console.WriteLine("<int>.Property => " + ((MyTestClass <int>)testClass).Property);

            System.Console.WriteLine("<int>.Property => " + casted.Property);

            setter = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.LoadSetter(() => casted.AnotherProperty.Equals(null));

            System.Console.WriteLine("<int>.AnotherProperty => " + ((MyTestClass <int>)testClass).AnotherProperty);

            System.Console.WriteLine("<int>.AnotherProperty => " + casted.AnotherProperty);

            setter.Invoke(testClass, new object[] { 2 });

            if (false == (casted.AnotherProperty == 2))
            {
                throw new System.Exception("Did not invoke setter");
            }

            System.Console.WriteLine("<int>.AnotherProperty => " + ((MyTestClass <int>)testClass).AnotherProperty);

            System.Console.WriteLine("<int>.AnotherProperty => " + casted.AnotherProperty);

            System.Reflection.TypeInfo typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => new MyTestClass <int>());

            if (typeInfo.GetGenericArguments()[0] != typeof(int))
            {
                throw new System.Exception("Not correct type");
            }

            System.Console.WriteLine("TypeInfo.Name " + typeInfo.Name);

            System.Console.WriteLine("TypeInfo.MetadataToken " + typeInfo.MetadataToken);

            if (typeInfo.GetGenericArguments()[0] != typeof(int))
            {
                throw new System.Exception("Not correct type");
            }

            typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (typeof(MyTestClass <int>)).GetType());

            if (typeInfo.GetGenericArguments()[0] != typeof(int))
            {
                throw new System.Exception("Not correct type");
            }

            System.Type unboundedType = typeof(MyTestClass <>);

            typeInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetTypeInfo(() => (unboundedType).GetType());

            System.Console.WriteLine("TypeInfo.Name " + typeInfo.Name);

            System.Console.WriteLine("TypeInfo.MetadataToken " + typeInfo.MetadataToken);

            System.Reflection.MethodInfo methodInfo = Media.Common.Extensions.ExpressionExtensions.SymbolExtensions.GetMethodInfo(() => ((MyTestClass <int>)null).Method());

            if (false.Equals(methodInfo.DeclaringType.Equals(typeof(MyTestClass <int>))))
            {
                throw new System.Exception("Not correct type");
            }

            System.Console.WriteLine("GetMethodInfo.Name " + methodInfo.Name);

            System.Console.WriteLine("GetMethodInfo.MetadataToken " + methodInfo.MetadataToken);
        }