コード例 #1
0
 /// <summary>
 /// Creates an expression slim to expression converter using the provided type space.
 /// </summary>
 /// <param name="typeSpace">A type space, potentially containing pre-mapped types.</param>
 /// <param name="factory">The expression factory to use for expression creation.</param>
 public ExpressionSlimToExpressionConverter(InvertedTypeSpace typeSpace, IExpressionFactory factory)
 {
     TypeSpace  = typeSpace ?? throw new ArgumentNullException(nameof(typeSpace));
     _factory   = factory ?? throw new ArgumentNullException(nameof(factory));
     _variables = new Dictionary <ParameterExpressionSlim, ParameterExpression>();
     _labels    = new Dictionary <LabelTargetSlim, LabelTarget>();
 }
コード例 #2
0
        protected static MemberInfo MemberInfoRoundtrip(MemberInfo m)
        {
            var ts  = new TypeSpace();
            var its = new InvertedTypeSpace();

            return(MemberInfoRoundtrip(m, ts, its));
        }
コード例 #3
0
        protected static Type TypeRoundtrip(Type t)
        {
            var ts  = new TypeSpace();
            var its = new InvertedTypeSpace();

            return(TypeRoundtrip(t, ts, its));
        }
コード例 #4
0
        public void InvertedTypeSpace_ArgumentChecks()
        {
            var ts = new InvertedTypeSpace();

            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetMember(memberSlim: null), ex => Assert.AreEqual(ex.ParamName, "memberSlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetConstructor(constructorSlim: null), ex => Assert.AreEqual(ex.ParamName, "constructorSlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetField(fieldSlim: null), ex => Assert.AreEqual(ex.ParamName, "fieldSlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetMethod(methodSlim: null), ex => Assert.AreEqual(ex.ParamName, "methodSlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetProperty(propertySlim: null), ex => Assert.AreEqual(ex.ParamName, "propertySlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.ConvertType(type: null), ex => Assert.AreEqual(ex.ParamName, "type"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.MapType(typeSlim: null, type: null), ex => Assert.AreEqual(ex.ParamName, "typeSlim"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.MapType(SlimType, type: null), ex => Assert.AreEqual(ex.ParamName, "type"));
        }
コード例 #5
0
        public void InvertedTypeSpace_MapType_Success()
        {
            var ts      = new TypeSpace();
            var its     = new InvertedTypeSpace();
            var fooSlim = ts.ConvertType(typeof(Foo));

            its.MapType(fooSlim, typeof(Qux));

            var fooCtor = typeof(Foo).GetConstructors().Single();
            var quxCtor = typeof(Qux).GetConstructors().Single();

            Assert.AreEqual(quxCtor, MemberInfoRoundtrip(fooCtor, ts, its));

            var fooField = typeof(Foo).GetField("baz");
            var quxField = typeof(Qux).GetField("baz");

            Assert.AreEqual(quxField, MemberInfoRoundtrip(fooField, ts, its));

            var fooProp = typeof(Foo).GetProperty("Bar");
            var quxProp = typeof(Qux).GetProperty("Bar");

            Assert.AreEqual(quxProp, MemberInfoRoundtrip(fooProp, ts, its));

            var fooIdxProp = typeof(Foo).GetProperty("Item");
            var quxIdxProp = typeof(Qux).GetProperty("Item");

            Assert.AreEqual(quxIdxProp, MemberInfoRoundtrip(fooIdxProp, ts, its));

            var fooSimple = typeof(Foo).GetMethod("Qux1");
            var quxSimple = typeof(Qux).GetMethod("Qux1");

            Assert.AreEqual(quxSimple, MemberInfoRoundtrip(fooSimple, ts, its));

            var fooGenericDef = typeof(Foo).GetMethod("Qux2");
            var quxGenericDef = typeof(Qux).GetMethod("Qux2");

            Assert.AreEqual(quxGenericDef, MemberInfoRoundtrip(fooGenericDef, ts, its));

            var fooGeneric = fooGenericDef.MakeGenericMethod(new[] { typeof(int) });
            var quxGeneric = quxGenericDef.MakeGenericMethod(new[] { typeof(int) });

            Assert.AreEqual(quxGeneric, MemberInfoRoundtrip(fooGeneric, ts, its));
        }
コード例 #6
0
        public void InvertedTypeSpace_MemberRoundtrip_BadMapping_ThrowsInvalidOperation()
        {
            var ts      = new TypeSpace();
            var its     = new InvertedTypeSpace();
            var fooSlim = ts.ConvertType(typeof(Foo));

            its.MapType(fooSlim, typeof(Bar));

            var ctor = typeof(Foo).GetConstructors().Single();

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(ctor, ts, its));

            var field = typeof(Foo).GetField("baz");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(field, ts, its));

            var prop = typeof(Foo).GetProperty("Bar");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(prop, ts, its));

            var idxProp = typeof(Foo).GetProperty("Item");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(idxProp, ts, its));

            var simple = typeof(Foo).GetMethod("Qux1");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(simple, ts, its));

            var genericDef = typeof(Foo).GetMethod("Qux2");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(genericDef, ts, its));

            var generic = genericDef.MakeGenericMethod(new[] { typeof(int) });

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(generic, ts, its));
        }
コード例 #7
0
 /// <summary>
 /// Creates an expression slim to expression converter using the provided type space.
 /// </summary>
 /// <param name="typeSpace">A type space, potentially containing pre-mapped types.</param>
 public ExpressionSlimToExpressionConverter(InvertedTypeSpace typeSpace)
     : this(typeSpace, ExpressionFactory.Instance)
 {
 }
コード例 #8
0
 protected static MemberInfo MemberInfoRoundtrip(MemberInfo m, TypeSpace ts, InvertedTypeSpace its)
 {
     return(its.GetMember(ts.GetMember(m)));
 }
コード例 #9
0
 protected static Type TypeRoundtrip(Type t, TypeSpace ts, InvertedTypeSpace its)
 {
     return(its.ConvertType(ts.ConvertType(t)));
 }
コード例 #10
0
 public DataModelBonsaiExpressionSerializer(InvertedTypeSpace invertedTypeSpace, Func <Type, Func <object, JsonExpression> > liftFactory, Func <Type, Func <JsonExpression, object> > reduceFactory, Version version)
     : base(liftFactory, reduceFactory, version)
 {
     _reducer = new ExpressionSlimToExpressionConverter(invertedTypeSpace);
 }