Exemple #1
0
        internal static ConstructorInfo GetValueTypeCtor(this Type type, ref List <Expression> args)
        {
            var cs = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var item in cs)
            {
                var pars = item.GetParameters();
                JsonDeserializeCtorAttribute jsonDeserializeCtorAttribute = item.GetCustomAttribute <JsonDeserializeCtorAttribute>();
                if (jsonDeserializeCtorAttribute != null)
                {
                    args = new List <Expression>();

                    for (int i = 0; i < jsonDeserializeCtorAttribute._args.Length; i++)
                    {
                        args.Add(Expression.Constant(jsonDeserializeCtorAttribute._args[i], pars[i].ParameterType));
                    }
                    return(item);
                }
            }
            return(null);
        }
Exemple #2
0
        internal static ConstructorInfo GetClassCtor(this Type type, ref List <Expression> args)
        {
            var               cs = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            ConstructorInfo   attrCtor = null, publicNoArgsCtor = null, privateNoArgsCtor = null, publicArgsCtor = null, privateArgsCtor = null;
            List <Expression> attrCtorArgs = null; ParameterInfo[] publicArgsCtorParas = null, privateArgsCtorParas = null;

            foreach (var item in cs)
            {
                var pars = item.GetParameters();
                JsonDeserializeCtorAttribute jsonDeserializeCtorAttribute = item.GetCustomAttribute <JsonDeserializeCtorAttribute>();
                if (jsonDeserializeCtorAttribute != null)
                {
                    attrCtorArgs = new List <Expression>();
                    attrCtor     = item;

                    for (int i = 0; i < jsonDeserializeCtorAttribute._args.Length; i++)
                    {
                        attrCtorArgs.Add(Expression.Constant(jsonDeserializeCtorAttribute._args[i], pars[i].ParameterType));
                    }

                    break;
                }

                if (item.IsPublic)
                {
                    if (pars.Length == 0)
                    {
                        publicNoArgsCtor = item;
                    }
                    else if (publicArgsCtor == null || pars.Length < publicArgsCtor.GetParameters().Length)
                    {
                        publicArgsCtor      = item;
                        publicArgsCtorParas = pars;
                    }
                }
                else if (item.IsPrivate)
                {
                    if (pars.Length == 0)
                    {
                        privateNoArgsCtor = item;
                    }
                    else if (privateArgsCtor == null || pars.Length < privateArgsCtor.GetParameters().Length)
                    {
                        privateArgsCtor      = item;
                        privateArgsCtorParas = pars;
                    }
                }
            }


            if (attrCtor != null)
            {
                args = attrCtorArgs;
                return(attrCtor);
            }
            if (publicNoArgsCtor != null)
            {
                args = null;
                return(publicNoArgsCtor);
            }
            if (privateNoArgsCtor != null)
            {
                args = null;
                return(privateNoArgsCtor);
            }
            if (publicArgsCtor != null)
            {
                args = new List <Expression>();
                for (int i = 0; i < publicArgsCtorParas.Length; i++)
                {
                    object value    = null;
                    var    paraType = publicArgsCtorParas[i].ParameterType;
                    if (paraType.IsValueType)
                    {
                        value = Activator.CreateInstance(paraType);
                    }
                    args.Add(Expression.Constant(value, paraType));
                }
                return(publicArgsCtor);
            }
            else //(privateArgsCtor != null)
            {
                args = new List <Expression>();
                for (int i = 0; i < publicArgsCtorParas.Length; i++)
                {
                    object value    = null;
                    var    paraType = publicArgsCtorParas[i].ParameterType;
                    if (paraType.IsValueType)
                    {
                        value = Activator.CreateInstance(paraType);
                    }
                    args.Add(Expression.Constant(value, paraType));
                }
                return(privateArgsCtor);
            }
        }