Пример #1
0
        public static void ToStringTest()
        {
            MemberInitExpression e1 = Expression.MemberInit(Expression.New(typeof(Y)), Expression.Bind(typeof(Y).GetProperty(nameof(Y.Z)), Expression.Parameter(typeof(int), "z")));

            Assert.Equal("new Y() {Z = z}", e1.ToString());

            MemberInitExpression e2 = Expression.MemberInit(Expression.New(typeof(Y)), Expression.Bind(typeof(Y).GetProperty(nameof(Y.Z)), Expression.Parameter(typeof(int), "z")), Expression.Bind(typeof(Y).GetProperty(nameof(Y.A)), Expression.Parameter(typeof(int), "a")));

            Assert.Equal("new Y() {Z = z, A = a}", e2.ToString());

            MemberInitExpression e3 = Expression.MemberInit(Expression.New(typeof(X)), Expression.MemberBind(typeof(X).GetProperty(nameof(X.Y)), Expression.Bind(typeof(Y).GetProperty(nameof(Y.Z)), Expression.Parameter(typeof(int), "z"))));

            Assert.Equal("new X() {Y = {Z = z}}", e3.ToString());

            MemberInitExpression e4 = Expression.MemberInit(Expression.New(typeof(X)), Expression.MemberBind(typeof(X).GetProperty(nameof(X.Y)), Expression.Bind(typeof(Y).GetProperty(nameof(Y.Z)), Expression.Parameter(typeof(int), "z")), Expression.Bind(typeof(Y).GetProperty(nameof(Y.A)), Expression.Parameter(typeof(int), "a"))));

            Assert.Equal("new X() {Y = {Z = z, A = a}}", e4.ToString());

            Reflection.MethodInfo add = typeof(List <int>).GetMethod(nameof(List <int> .Add));

            MemberInitExpression e5 = Expression.MemberInit(Expression.New(typeof(X)), Expression.ListBind(typeof(X).GetProperty(nameof(X.XS)), Expression.ElementInit(add, Expression.Parameter(typeof(int), "a"))));

            Assert.Equal("new X() {XS = {Void Add(Int32)(a)}}", e5.ToString());

            MemberInitExpression e6 = Expression.MemberInit(Expression.New(typeof(X)), Expression.ListBind(typeof(X).GetProperty(nameof(X.XS)), Expression.ElementInit(add, Expression.Parameter(typeof(int), "a")), Expression.ElementInit(add, Expression.Parameter(typeof(int), "b"))));

            Assert.Equal("new X() {XS = {Void Add(Int32)(a), Void Add(Int32)(b)}}", e6.ToString());
        }
        internal DesignerVerb GetDesignerVerb(object obj, Reflection.MethodInfo mi)
        {
            EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), obj, mi);

            if (cmdId != null)
            {
                return(new DesignerVerb(menuText, handler, cmdId));
            }
            return(new DesignerVerb(menuText, handler));
        }
Пример #3
0
        public void CantBeAction(Type[] typeArgs)
        {
            Type delType = Expression.GetDelegateType(typeArgs.Append(typeof(void)).ToArray());

            Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
            Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
            Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
            Reflection.MethodInfo method = delType.GetMethod("Invoke");
            Assert.Equal(typeof(void), method.ReturnType);
            Assert.Equal(typeArgs, method.GetParameters().Select(p => p.ParameterType));
        }
Пример #4
0
        public void CantBeFunc(Type[] typeArgs)
        {
#if !FEATURE_COMPILE
            Assert.Throws <PlatformNotSupportedException>(() => Expression.GetDelegateType(typeArgs));
#else
            Type delType = Expression.GetDelegateType(typeArgs);
            Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
            Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
            Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
            Reflection.MethodInfo method = delType.GetMethod("Invoke");
            Assert.Equal(typeArgs.Last(), method.ReturnType);
            Assert.Equal(typeArgs.Take(typeArgs.Length - 1), method.GetParameters().Select(p => p.ParameterType));
#endif
        }
Пример #5
0
 public void CantBeFunc(Type[] typeArgs)
 {
     if (PlatformDetection.IsLinqExpressionsBuiltWithIsInterpretingOnly)
     {
         Assert.Throws <PlatformNotSupportedException>(() => Expression.GetDelegateType(typeArgs));
     }
     else
     {
         Type delType = Expression.GetDelegateType(typeArgs);
         Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
         Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
         Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
         Reflection.MethodInfo method = delType.GetMethod("Invoke");
         Assert.Equal(typeArgs.Last(), method.ReturnType);
         Assert.Equal(typeArgs.Take(typeArgs.Length - 1), method.GetParameters().Select(p => p.ParameterType));
     }
 }
Пример #6
0
 public void CantBeFunc(Type[] typeArgs)
 {
     if (!RuntimeFeature.IsDynamicCodeSupported)
     {
         Assert.Throws <PlatformNotSupportedException>(() => Expression.GetDelegateType(typeArgs));
     }
     else
     {
         Type delType = Expression.GetDelegateType(typeArgs);
         Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
         Assert.DoesNotContain("System.Action", delType.FullName);
         Assert.DoesNotContain("System.Func", delType.FullName);
         Reflection.MethodInfo method = delType.GetMethod("Invoke");
         Assert.Equal(typeArgs.Last(), method.ReturnType);
         Assert.Equal(typeArgs.Take(typeArgs.Length - 1), method.GetParameters().Select(p => p.ParameterType));
     }
 }
Пример #7
0
 public void CantBeAction(Type[] typeArgs)
 {
     Type[] delegateArgs = typeArgs.Append(typeof(void)).ToArray();
     if (!RuntimeFeature.IsDynamicCodeSupported)
     {
         Assert.Throws <PlatformNotSupportedException>(() => Expression.GetDelegateType(delegateArgs));
     }
     else
     {
         Type delType = Expression.GetDelegateType(delegateArgs);
         Assert.True(typeof(MulticastDelegate).IsAssignableFrom(delType));
         Assert.DoesNotMatch(new Regex(@"System\.Action"), delType.FullName);
         Assert.DoesNotMatch(new Regex(@"System\.Func"), delType.FullName);
         Reflection.MethodInfo method = delType.GetMethod("Invoke");
         Assert.Equal(typeof(void), method.ReturnType);
         Assert.Equal(typeArgs, method.GetParameters().Select(p => p.ParameterType));
     }
 }
Пример #8
0
        public void ExplicitContinue(bool useInterpreter)
        {
            var builder = new StringBuilder();
            ParameterExpression value     = Expression.Variable(typeof(int));
            LabelTarget         @break    = Expression.Label();
            LabelTarget         @continue = Expression.Label();

            Reflection.MethodInfo append = typeof(StringBuilder).GetMethod(nameof(StringBuilder.Append), new[] { typeof(int) });
            Action act = Expression.Lambda <Action>(
                Expression.Block(
                    new[] { value },
                    Expression.Assign(value, Expression.Constant(0)),
                    Expression.Loop(
                        Expression.Block(
                            Expression.PostIncrementAssign(value),
                            Expression.IfThen(
                                Expression.GreaterThanOrEqual(value, Expression.Constant(10)),
                                Expression.Break(@break)
                                ),
                            Expression.IfThen(
                                Expression.Equal(
                                    Expression.Modulo(value, Expression.Constant(2)),
                                    Expression.Constant(0)
                                    ),
                                Expression.Continue(@continue)
                                ),
                            Expression.Call(Expression.Constant(builder), append, value)
                            ),
                        @break,
                        @continue
                        )
                    )
                ).Compile(useInterpreter);

            act();
            Assert.Equal("13579", builder.ToString());
        }