Esempio n. 1
0
        /// <summary>
        /// 动态设置指定名称的成员的值。
        /// </summary>
        /// <param name="name">成员的名称,可以是一个属性或字段。</param>
        /// <param name="value">成员的值。</param>
        public virtual void Set(string name, object value)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var memberInfo        = Type.GetMember(name, AllBindingFalgs).FirstOrDefault();
            DynamicMemberSetter d = null;

            if (memberInfo != null)
            {
                if (memberInfo.MemberType == MemberTypes.Property)
                {
                    d = DynamicFactory.CreatePropertySetter(memberInfo as PropertyInfo);
                }
                else if (memberInfo.MemberType == MemberTypes.Field)
                {
                    d = DynamicFactory.CreateFieldSetter(memberInfo as FieldInfo);
                }
            }

            if (d == null)
            {
                throw new MissingMemberException(Type.FullName, name);
            }
            d(Instance, value);
        }
Esempio n. 2
0
        /// <summary>
        /// 动态调用指定名称的泛型方法。
        /// </summary>
        /// <param name="genericTypes">泛型</param>
        /// <param name="name">方法的名称。</param>
        /// <param name="args">方法的参数值。</param>
        /// <returns>方法执行的结果,如果方法是一个 void 签名则返回 null 值。</returns>
        public virtual object Call(Type[] genericTypes, string name, params object[] args)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (genericTypes == null)
            {
                throw new ArgumentNullException(nameof(genericTypes));
            }
            if (genericTypes.Length == 0)
            {
                return(Call(name, args));
            }

            var key        = name + genericTypes.Join(t => t.Name + t.MetadataToken, ", ", "[", "]");
            var methodInfo = Type.GetMethod(name, AllBindingFalgs);

            if (methodInfo == null)
            {
                throw new MissingMethodException(Type.FullName, key);
            }
            if (!methodInfo.IsGenericMethodDefinition)
            {
                throw new ArgumentException("仅支持尚未构造泛型参数的方法。", name);
            }

            var d = DynamicFactory.CreateMethodInvoker(methodInfo.MakeGenericMethod(genericTypes));

            return(d(Instance, args));
        }
Esempio n. 3
0
        /// <summary>
        /// 指定预期和实际服务类型,创建一个实例构造器的回调函数。
        /// </summary>
        /// <param name="expectType">预期服务类型。</param>
        /// <param name="actualType">实际服务类型。</param>
        /// <returns>实例构造器委托。</returns>
        internal InstanceCreatorCallback CreateCallback(Type expectType, Type actualType)
        {
            this.TestActualType(actualType);

            var ctors = actualType.GetConstructors(Aoite.Reflection.Flags.InstanceAnyVisibility);

            if (ctors.Length > 1)
            {
                ctors = ctors.OrderBy(ctor => ctor.GetAttribute <OrderMappingAttribute>()?.Order ?? 0).ToArray();
            }

            var hasExpectedType         = expectType != actualType;
            List <Exception> exceptions = new List <Exception>();

            foreach (var ctor in ctors)
            {
                var ps = ctor.GetParameters();
                if (ps.Length == 0)
                {
                    return(lmvs => Activator.CreateInstance(actualType, true));
                }

                var callSites = new ICallSite[ps.Length];
                for (int i = 0, lastMappingIndex = 0; i < ps.Length; i++)
                {
                    var       p            = ps[i];
                    var       pType        = p.ParameterType;
                    var       isSimpleType = pType.IsSimpleType();
                    var       pName        = p.Name;
                    ICallSite callSite     = null;
                    if (p.IsDefined(LastMappingAttribute.Type, true) ||
                        (pType.IsDefined(LastMappingAttribute.Type, true) && !p.IsDefined(IgnoreAttribute.Type, true)))
                    {
                        callSite = new LastMappingCallSite(actualType, p, lastMappingIndex++);
                    }
                    else if (!this.FindParameterCallSite(expectType, actualType, pType, pName, hasExpectedType, isSimpleType, out callSite))
                    {
                        exceptions.Add(new ArgumentException($"实际服务类型{actualType.FullName}():构造函数的参数“{pName}”尚未配置映射,并且自动分析失败。", pName));
                        callSites = null;
                        break;
                    }

                    callSites[i] = callSite;
                }

                if (callSites != null)
                {
                    var ctorHandler = DynamicFactory.CreateConstructorHandler(ctor);
                    return(lmps => ctorHandler(callSites.Each(cs => cs.Invoke(lmps))));
                }
            }
            throw new AggregateException(actualType.FullName + "没有在映射列表中找到匹配的构造函数。错误内容可以详见 System.Exception.Data 字典。", exceptions);
        }
Esempio n. 4
0
        private static Tuple <DynamicMethodInvoker, DynamicMethodInvoker> GetInvoker <TResult>(ICommand <TResult> command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            return(Invokers.GetOrAdd(command.GetType(), type =>
            {
                var flags = Reflection.BindingFlags.Static | Reflection.BindingFlags.Public;
                var resultType = typeof(TResult);
                return Tuple.Create(DynamicFactory.CreateMethodInvoker(CommandBusWapperType.MakeGenericType(type, resultType).GetMethod("Execute", flags))
                                    , DynamicFactory.CreateMethodInvoker(CommandBusWapperType.MakeGenericType(type, resultType).GetMethod("ExecuteAsync", flags)));
            }
                                     ));
        }
Esempio n. 5
0
        /// <summary>
        /// 动态调用指定名称的方法。
        /// </summary>
        /// <param name="name">方法的名称。</param>
        /// <param name="args">方法的参数值。</param>
        /// <returns>方法执行的结果,如果方法是一个 void 签名则返回 null 值。</returns>
        public virtual object Call(string name, params object[] args)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var methodInfo = Type.GetMethod(name, AllBindingFalgs);

            if (methodInfo == null)
            {
                throw new MissingMethodException(Type.FullName, name);
            }
            var d = DynamicFactory.CreateMethodInvoker(methodInfo);

            return(d(Instance, args));
        }
Esempio n. 6
0
 /// <summary>
 /// 将dynamic类型的对象传递到view页面
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static object ToDynamic(this object entity)
 {
     return(DynamicFactory.ToDynamic(entity));
 }