Exemple #1
0
        private static void CopyBySource(IDictionary <string, string> src, object dest, bool throwOnNotExist)
        {
            IReflector r = Reflector.Bind(dest);

            foreach (var key in src.Keys)
            {
                if (!r.ExistProperty(key))
                {
                    if (throwOnNotExist)
                    {
                        ExceptionHelper.ThrowDestPropertyNotExist(dest.GetType(), key);
                    }
                    else
                    {
                        _log.Warning("Destination type [{0}] does not have property [{1}].", dest.GetType(), key);
                    }
                }
                else
                {
                    try
                    {
                        Type   pType = r.GetPropertyType(key);
                        object val   = TypeCast.ChangeToTypeOrNullableType(src[key], pType);
                        r.SetPropertyValue(key, val);
                    }
                    catch (Exception ex)
                    {
                        throw ex.CreateWrapException <CopyException>();
                    }
                }
            }
        }
Exemple #2
0
        public ISmartInvoker CreateSmartInvoker()
        {
            Type       t = Reflector.LoadType("RexToy.ExpressionLanguage.EvalSmartInvoker,RexToy.Core.Extension");
            IReflector r = Reflector.Bind(t, ReflectorPolicy.TypePublic);

            return((ISmartInvoker)r.Invoke("CreateInstance", new object[] { new SampleFuncOverload(), true }));
        }
Exemple #3
0
        public bool ReadyToBuild(IObjectBuildContext ctx)
        {
            Type       targetType = (ctx.ComponentType != null) ? ctx.ComponentType : ctx.ServiceType;
            IReflector r          = Reflector.Bind(targetType, ReflectorPolicy.TypePublic);

            return(PolicyUtility.IsReady(_propertyValue, r.GetPropertyType(_propertyName), ctx));
        }
Exemple #4
0
        private static void CopyByDest(IDictionary <string, string> src, object dest, bool throwOnNotExist)
        {
            IReflector r = Reflector.Bind(dest);

            foreach (var property in r.FindAllPropertyNames())
            {
                if (src.ContainsKey(property))
                {
                    try
                    {
                        Type   pType = r.GetPropertyType(property);
                        object val   = TypeCast.ChangeToTypeOrNullableType(src[property], pType);
                        r.SetPropertyValue(property, val);
                    }
                    catch (Exception ex)
                    {
                        throw ex.CreateWrapException <CopyException>();
                    }
                }
                else
                {
                    if (throwOnNotExist)
                    {
                        ExceptionHelper.ThrowSourceKeyOrPropertyNotExist(property);
                    }
                    else
                    {
                        _log.Warning("Source dictionary does not have key [{0}].", property);
                    }
                }
            }
        }
Exemple #5
0
        protected override void BuildCreateTableColumns()
        {
            IReflector r = Reflector.Bind(_map.EntityType, ReflectorPolicy.CreateInstance(false, false, false, false));

            foreach (var pkInfo in _map.PrimaryKeyMaps)
            {
                BuildColumnBasic(r, pkInfo, true);
                if (_map.PrimaryKeyGenerate == PrimaryKeyGenerate.Auto)
                {
                    _str.Append(" AUTO_INCREMENT");
                }
                _str.Append(_tr.ColumnDelimiter);
            }

            foreach (var nonPkInfo in _map.NonPKPropertyMaps)
            {
                BuildColumnBasic(r, nonPkInfo, false);
                _str.Append(_tr.ColumnDelimiter);
            }

            _str.Append(_tr.PrimaryKey);
            _str.Append(StringPair.Parenthesis.Begin);
            foreach (var pkInfo in _map.PrimaryKeyMaps)
            {
                _str.Append(_tr.GetEscapedColumnName(pkInfo.ColumnName)).Append(_tr.ColumnDelimiter);
            }
            _str.RemoveEnd(_tr.ColumnDelimiter);
            _str.Append(StringPair.Parenthesis.End);
        }
Exemple #6
0
        public void TestInvokeVarargProvideMany()
        {
            IReflector r      = Reflector.Bind(new ReflectSampleClass2());
            object     result = r.Invoke("Acc", new object[] { 1, 2, 3, 4, 5 });

            Assert.AreEqual(15, result);
        }
Exemple #7
0
        public void TestInvokeOmitOptParam()
        {
            IReflector r      = Reflector.Bind(new ReflectSampleClass2());
            object     result = r.Invoke("Mod", new object[] { 5 });

            Assert.AreEqual(1, result);
        }
Exemple #8
0
        internal object Render(Type type, bool ignoreTypeSafe)
        {
            if (!type.HasDefaultConstructor())
            {
                ExceptionHelper.ThrowNoDefaultConstructor(type);
            }

            IExtendConverter cvt = ExtendConverter.Instance();

            if (cvt.CanConvert(type))
            {
                return(cvt.FromJson(type, this, ignoreTypeSafe));
            }

            object     instance = Activator.CreateInstance(type, true);
            IReflector r        = Reflector.Bind(instance, ReflectorPolicy.InstancePublicIgnoreCase);

            foreach (KeyValuePair <string, object> kvp in _dict)
            {
                string propertyName = kvp.Key;
                Type   targetType   = r.GetPropertyType(propertyName);
                var    data         = JsonHelper.Render(kvp.Value, targetType, ignoreTypeSafe);
                r.SetPropertyValue(propertyName, data);
            }

            return(instance);
        }
Exemple #9
0
        public override void BuildUp(IObjectBuildContext ctx)
        {
            if (ctx.SkipCreationPolicy)
            {
                _log.Verbose("Factory policy is skipped.");
                return;
            }

            IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypeAll);

            MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(_methodName));
            if (methods.Length == 0)
            {
                ExceptionHelper.ThrowNoValidMethod(_factoryType, _methodName);
            }
            foreach (MethodBase method in methods)
            {
                if (IsReady(method, ctx))
                {
                    object[] args     = PrepareArgs(ctx, method);
                    object   instance = r.Invoke(_methodName, args);
                    ctx.Instance = instance;
                    _log.Verbose("Factory policy create an instance of type [{0}].", instance.GetType());
                    return;
                }
            }

            _log.Debug("Factory policy create instance fail.");
            ExceptionHelper.ThrowMethodNotReady(_factoryType, _methodName);
        }
Exemple #10
0
        protected override void BuildCreateTableColumns()
        {
            IReflector r = Reflector.Bind(_map.EntityType, ReflectorPolicy.CreateInstance(false, false, false, false));

            foreach (var pkInfo in _map.PrimaryKeyMaps)
            {
                BuildColumnBasic(r, pkInfo, true);
                //Note:GUID is not supported
                if (_map.PrimaryKeyGenerate == PrimaryKeyGenerate.Auto)
                {
                    _str.Append("IDENTITY(1, 1) ");
                }
                _str.Append(_tr.Not).Append(_tr.Null).Append(_tr.ColumnDelimiter);
            }

            foreach (var nonPkInfo in _map.NonPKPropertyMaps)
            {
                BuildColumnBasic(r, nonPkInfo, false);
                _str.Append(_tr.ColumnDelimiter);
            }

            _str.Append(_tr.Constraint).Append(_tr.GetEscapedColumnName(PK_PREFIX + _map.Table.LocalName)).Append(_tr.PrimaryKey);
            _str.Append(StringPair.Parenthesis.Begin);
            foreach (var pkInfo in _map.PrimaryKeyMaps)
            {
                _str.Append(_tr.GetEscapedColumnName(pkInfo.ColumnName)).Append(_tr.ColumnDelimiter);
            }
            _str.RemoveEnd(_tr.ColumnDelimiter);
            _str.Append(StringPair.Parenthesis.End);
        }
Exemple #11
0
        public void TestAccessProperty_Type()
        {
            IReflector r = Reflector.Bind(typeof(ReflectSampleClass1), ReflectorPolicy.TypeAllIgnoreCase);

            Assert.IsTrue(r.ExistProperty("P"));
            Assert.IsTrue(r.ExistProperty("p"));
        }
Exemple #12
0
        public void TestAccessField_Type()
        {
            IReflector r = Reflector.Bind(typeof(ReflectSampleClass1), ReflectorPolicy.TypeAll);

            Assert.IsTrue(r.ExistField("V"));
            Assert.IsFalse(r.ExistField("v"));
        }
Exemple #13
0
        public virtual StringBuilder BuildInsertValues(IObjectMapInfo map, object entity)
        {
            StringBuilder b = new StringBuilder();
            IReflector    r = Reflector.Bind(entity);

            foreach (var pmi in map.PropertyMaps)
            {
                if (map.PrimaryKeyGenerate == PrimaryKeyGenerate.Auto && map.PrimaryKeyMaps.Contains(pmi))
                {
                    continue;
                }

                //Note:special handling Guid when insert, must use '*', not same as WHERE {Guid{*}}
                object val = r.GetPropertyValue(pmi.PropertyName);
                if (val is Guid)
                {
                    b.Append(_tr.GetValueString(val.ToString())).Append(_tr.ColumnDelimiter);
                }
                else
                {
                    b.Append(_tr.GetValueString(val)).Append(_tr.ColumnDelimiter);
                }
            }
            b.RemoveEnd(_tr.ColumnDelimiter);
            return(b);
        }
Exemple #14
0
        public void TestInvoke()
        {
            IReflector r      = Reflector.Bind(new ReflectSampleClass2());
            object     result = r.Invoke("Sum", new object[] { 1, 3 });

            Assert.AreEqual(4, result);
        }
Exemple #15
0
        public void TearDown()
        {
            IReflector r  = Reflector.Bind(_k, ReflectorPolicy.InstanceAll);
            object     _c = r.GetFieldValue("_lifecycleContainer");
            ConcurrentDictionary <object, IObjectBuildContext> c = _c as ConcurrentDictionary <object, IObjectBuildContext>;

            Assert.AreEqual(0, c.Count);
        }
Exemple #16
0
        private SampleWriter GetWriter(ILog log)
        {
            object       obj    = Reflector.Bind(log, ReflectorPolicy.InstanceAll).GetFieldValue("_writer");
            SampleWriter writer = obj as SampleWriter;

            Assert.IsNotNull(writer);
            return(writer);
        }
Exemple #17
0
        public override object Visit(IndexerNode obj)
        {
            object instance = obj.Variable.Accept(this);

            object[] paramArray = (object[])obj.Args.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            ISmartInvoker   invoker;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                invoker = EvalSmartInvoker.CreateInstance(cdef.ObjType, false);
            }
            else
            {
                invoker = EvalSmartInvoker.CreateInstance(instance, true);
            }

            if (instance.GetType().IsArray)//Note:invoke "Get" on array
            {
                return(invoker.Invoke("Get", paramArray));
            }
            else//Note:invoke indexer on instance
            {
                try//Note:Try indexer first
                {
                    return(invoker.Invoke("get_Item", paramArray));
                }
                catch//Note:then try property
                {
                    if (paramArray.Length == 1 && paramArray[0] != null)
                    {
                        string     propertyName = paramArray[0].ToString();
                        IReflector r            = Reflector.Bind(instance);
                        if (r.ExistProperty(propertyName))
                        {
                            return(r.GetPropertyValue(propertyName));
                        }
                        else if (r.ExistField(propertyName))
                        {
                            return(r.GetFieldValue(propertyName));
                        }
                        else
                        {
                            ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName);
                        }
                    }
                    //Note:Can not convert to property, throw
                    throw;
                }
            }
        }
Exemple #18
0
        public void TestAccessIndexer()
        {
            IReflector r = Reflector.Bind(new ReflectSampleClass1(), ReflectorPolicy.InstancePublic);

            Assert.AreEqual(5, r.GetIndexerValue(new object[] { 5 }));

            r.SetIndexerValue(new object[] { 3 }, 4);

            var cs = r.FindAllConstructors();
        }
Exemple #19
0
        public void BuildUp(IObjectBuildContext ctx)
        {
            if (ctx.SkipInitializationPolicy)
            {
                return;
            }

            IReflector r   = Reflector.Bind(ctx.Instance);
            object     val = PolicyUtility.Build(_propertyValue, r.GetPropertyType(_propertyName), ctx);

            r.SetPropertyValue(_propertyName, val);
        }
Exemple #20
0
        private static void CopyByBoth(object src, object dest)
        {
            IReflector rSrc  = Reflector.Bind(src);
            IReflector rDest = Reflector.Bind(dest);

            foreach (var pName in rSrc.FindAllPropertyNames())
            {
                if (rDest.ExistProperty(pName))
                {
                    rDest.SetPropertyValue(pName, rSrc.GetPropertyValue(pName));
                }
            }
        }
Exemple #21
0
        public virtual StringBuilder BuildUpdateSets(IObjectMapInfo map, object entity)
        {
            StringBuilder b = new StringBuilder();
            IReflector    r = Reflector.Bind(entity);

            foreach (var pmi in map.NonPKPropertyMaps)
            {
                b.Append(_tr.GetEscapedColumnName(pmi.ColumnName));
                b.Append(_tr.Equal).Append(_tr.GetValueString(r.GetPropertyValue(pmi.PropertyName)));
                b.Append(_tr.ColumnDelimiter);
            }
            b.RemoveEnd(_tr.ColumnDelimiter);
            return(b);
        }
Exemple #22
0
        public void TestAccessProperty_InstancePrivate()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstanceAllIgnoreCase);
            Type t = r.GetPropertyType("Name");

            Assert.IsTrue(t == typeof(string));

            object val = r.GetPropertyValue("Name");

            Assert.AreEqual("sample1", val);

            Assert.IsTrue(r.ExistProperty("name"));
            Assert.IsTrue(r.ExistProperty("Name"));

            Assert.IsFalse(r.ExistProperty("P"));
        }
Exemple #23
0
        public void TestAccessField_InstancePrivate()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstanceAll);
            Type t = r.GetFieldType("_name");

            Assert.IsTrue(t == typeof(string));

            object val = r.GetFieldValue("_name");

            Assert.AreEqual("sample1", val);

            Assert.IsTrue(r.ExistField("_name"));
            Assert.IsFalse(r.ExistField("_Name"));

            Assert.IsFalse(r.ExistField("V"));
        }
Exemple #24
0
        private static void CopyByDest(object src, object dest, bool throwOnNotExist)
        {
            IReflector rSrc  = Reflector.Bind(src);
            IReflector rDest = Reflector.Bind(dest);

            foreach (var pName in rDest.FindAllPropertyNames())
            {
                if (!rSrc.ExistProperty(pName))
                {
                    if (throwOnNotExist)
                    {
                        ExceptionHelper.ThrowSourceKeyOrPropertyNotExist(pName);
                    }
                }

                rDest.SetPropertyValue(pName, rSrc.GetPropertyValue(pName));
            }
        }
Exemple #25
0
        public void TestAccessProperty_InstancePublic()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstancePublicIgnoreCase);
            Type t = r.GetPropertyType("count");

            Assert.IsTrue(t == typeof(int));

            object val = r.GetPropertyValue("Count");

            Assert.AreEqual(3, val);

            Assert.IsTrue(r.ExistProperty("count"));
            Assert.IsTrue(r.ExistProperty("Count"));

            r.SetPropertyValue("count", 10);
            Assert.AreEqual(10, o.Count);
        }
Exemple #26
0
        public void TestAccessField_InstancePublic()
        {
            ReflectSampleClass1 o = new ReflectSampleClass1();
            IReflector          r = Reflector.Bind(o, ReflectorPolicy.InstancePublic);
            Type t = r.GetFieldType("X");

            Assert.IsTrue(t == typeof(int));

            object val = r.GetFieldValue("X");

            Assert.AreEqual(5, val);

            Assert.IsTrue(r.ExistField("X"));
            Assert.IsFalse(r.ExistField("x"));

            r.SetFieldValue("X", 10);
            Assert.AreEqual(10, o.X);
        }
Exemple #27
0
        public override object Visit(PropertyNode obj)
        {
            object instance = obj.Variable.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            string          propertyName = (obj.Property as SimpleNode).Token.TokenValue;
            IReflector      r;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                r = Reflector.Bind(cdef.ObjType, ReflectorPolicy.CreateInstance(false, true, false, false));
            }
            else
            {
                r = Reflector.Bind(instance, ReflectorPolicy.CreateInstance(false, true, false, true));
            }

            if (r.ExistProperty(propertyName))//Note:Property first
            {
                return(r.GetPropertyValue(propertyName));
            }
            else if (r.ExistField(propertyName))//Note:Field 2nd
            {
                return(r.GetFieldValue(propertyName));
            }
            else//Note:Then try indexer
            {
                try
                {
                    return(r.Invoke("get_Item", new object[] { propertyName }));
                }
                catch
                {
                    //Note:Try indexer failed.
                    ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName);
                    return(null);
                }
            }
        }
Exemple #28
0
        public override bool ReadyToBuild(IObjectBuildContext ctx)
        {
            IReflector r = Reflector.Bind(_factoryType, ReflectorPolicy.TypePublic);

            MethodBase[] methods = FilterByHintsAndSort(r.GetMethods(this._methodName));
            if (methods.Length == 0)
            {
                return(false);
            }

            foreach (MethodBase method in methods)
            {
                if (IsReady(method, ctx))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #29
0
        public override StringBuilder BuildInsertValues(MappingInfo.IObjectMapInfo map, object entity)
        {
            StringBuilder b = new StringBuilder();
            IReflector    r = Reflector.Bind(entity);

            foreach (var pmi in map.PropertyMaps)
            {
                if (map.PrimaryKeyGenerate == PrimaryKeyGenerate.Customized && map.PrimaryKeyMaps.Contains(pmi))
                {
                    string sequenceName = map.PKGenerateString.RemoveBegin(ConstantString.SEQ_Prefix);
                    b.Append(sequenceName).Append(_tr.MemberAccess).Append(ConstantString.SEQ_NextVal).Append(_tr.ColumnDelimiter);
                    continue;
                }

                b.Append(_tr.GetValueString(r.GetPropertyValue(pmi.PropertyName))).Append(_tr.ColumnDelimiter);
            }
            b.RemoveEnd(_tr.ColumnDelimiter);
            return(b);
        }
Exemple #30
0
        public void Initialize(Xml.XAccessor x)
        {
            string strHostType = x.GetStringValue(CLASS);

            if (string.IsNullOrEmpty(strHostType))
            {
                ExceptionHelper.ThrowPolicyInitNullError(this.GetType(), CLASS);
            }
            Type   hostType   = Reflector.LoadType(strHostType);
            string memberName = x.GetStringValue(MEMBER);

            if (string.IsNullOrEmpty(memberName))
            {
                ExceptionHelper.ThrowPolicyInitNullError(this.GetType(), MEMBER);
            }

            IReflector r = Reflector.Bind(hostType, ReflectorPolicy.TypePublic);

            _instance = r.GetPropertyOrFieldValue(memberName, false);
        }