Exemplo n.º 1
0
        public void AddBuilderMember(Type targetType, List <IBuilderMember> members)
        {
            var field = targetType.GetField(fieldName);

            //var field = targetType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetField| BindingFlags.CreateInstance| BindingFlags.Static| BindingFlags.GetField);
            if (field == null)
            {
                throw new InjectionException(string.Format("type <{0}> not found  field <{1}>", targetType.FullName, fieldName));
            }
            if (field.IsInitOnly)
            {
                throw new InjectionException(string.Format("type <{0}> field <{1}> is can't write", targetType.FullName, fieldName));
            }
            BuilderParameterInfo paramInfo;

            if (hasValue)
            {
                paramInfo = BuilderParameterInfo.MakeValue(field.FieldType, null, value);
            }
            else
            {
                paramInfo = InjectorUtils.FromAttributeProvider(field, field.FieldType);
            }

            members.Add(new FieldBuilder(field, paramInfo));
        }
Exemplo n.º 2
0
        public void AddBuilderMember(Type targetType, List <IBuilderMember> members)
        {
            var eventInfo = targetType.GetEvent(eventName);

            if (eventInfo == null)
            {
                throw new InjectionException(string.Format("type <{0}> not found  event <{1}>", targetType.FullName, eventName));
            }
            if (eventInfo.GetAddMethod(true) == null)
            {
                throw new InjectionException(string.Format("type <{0}> event <{1}> can't add", targetType.FullName, eventName));
            }

            BuilderParameterInfo paramInfo;

            if (hasValue)
            {
                paramInfo = BuilderParameterInfo.MakeValue(eventInfo.EventHandlerType, null, value);
            }
            else
            {
                paramInfo = InjectorUtils.FromAttributeProvider(eventInfo, eventInfo.EventHandlerType);
            }

            members.Add(new EventBuilder(eventInfo, paramInfo));
        }
Exemplo n.º 3
0
        public void AddBuilderMember(Type targetType, List <IBuilderMember> members)
        {
            var property = targetType.GetProperty(propertyName);

            if (property == null)
            {
                throw new InjectionException(string.Format("type <{0}> not found  property <{1}>", targetType.FullName, propertyName));
            }
            if (!property.CanWrite)
            {
                throw new InjectionException(string.Format("type <{0}> property <{1}> can't write", targetType.FullName, propertyName));
            }
            BuilderParameterInfo paramInfo;

            if (hasValue)
            {
                paramInfo = BuilderParameterInfo.MakeValue(property.PropertyType, null, value);
            }
            else
            {
                paramInfo = InjectorUtils.FromAttributeProvider(property, property.PropertyType);
            }


            members.Add(new PropertyBuilder(property, paramInfo));
        }
        internal ConstructorBuilder GetConstructorBuilder(Type targetType)
        {
            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance;
            var          argTypes     = injectParams.Select(o => o.ParameterType).ToArray();
            var          constructor  = InjectorUtils.FindConstructor(targetType, argTypes, bindingFlags);

            if (constructor == null)
            {
                throw new InjectionException(string.Format("type <{0}> not found  constructor, argument count:<{1}> ", targetType, argTypes.Length));
            }
            return(new ConstructorBuilder(constructor, injectParams));
        }
Exemplo n.º 5
0
        public void AddBuilderMember(Type targetType, List <IBuilderMember> members)
        {
            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod;

            var injectParams = BuilderParameterInfo.FromArgs(args);

            var methodBase = InjectorUtils.FindMethod(targetType, methodName, args, bindingFlags);

            if (methodBase == null)
            {
                throw new InjectionException(string.Format("type <{0}> not found  method <{1}>", targetType, methodName));
            }
            members.Add(new MethodBuilder(methodBase, injectParams));
        }
Exemplo n.º 6
0
        public static InjectTypeMetadata Get(Type type)
        {
            return(cached.GetOrAdd(type, (t) =>
            {
                InjectTypeMetadata typeInfo;

                typeInfo = new InjectTypeMetadata();
                typeInfo.type = type;

                if (type.IsPrimitive)
                {
                    typeInfo.IsPrimitive = true;
                    typeInfo.defaultValue = Activator.CreateInstance(type);
                }
                else if (type == typeof(string))
                {
                    typeInfo.IsPrimitive = true;
                    typeInfo.defaultValue = null;
                }

                if (typeInfo.IsPrimitive)
                {
                    return typeInfo;
                }


                BindingFlags bindingFlags = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.InvokeMethod;

                var members = type.GetMembers(bindingFlags);
                List <IBuilderMember> builderMembers = null;

                foreach (var member in members)
                {
                    var injAttr = member.GetCustomAttribute <InjectAttribute>(false);
                    if (injAttr == null)
                    {
                        continue;
                    }

                    IBuilderMember builderMember = null;

                    //if (injectionMember != null)
                    //    throw new Exception(string.Format("class {0}, {1} use multi", t.FullName, typeof(InjecAttribute).Name));

                    switch (member.MemberType)
                    {
                    case MemberTypes.Field:
                        var fInfo = (FieldInfo)member;
                        builderMember = new FieldBuilder(fInfo, InjectorUtils.FromAttributeProvider(fInfo, fInfo.FieldType));
                        break;

                    case MemberTypes.Property:
                        var pInfo = (PropertyInfo)member;
                        builderMember = new PropertyBuilder(pInfo, InjectorUtils.FromAttributeProvider(pInfo, pInfo.PropertyType));
                        break;

                    case MemberTypes.Method:
                        var mInfo = (MethodInfo)member;
                        builderMember = new MethodBuilder(mInfo, InjectorUtils.FromMethod(mInfo));
                        break;

                    case MemberTypes.Constructor:

                        var cInfo = (ConstructorInfo)member;
                        if (ValidateInjectConstructor(cInfo))
                        {
                            if (typeInfo.constructorBuilder != null)
                            {
                                throw new Exception(string.Format("class {0}, constructor only  use one attribute [{1}]", type.FullName, typeof(InjectAttribute).Name));
                            }

                            typeInfo.constructorBuilder = new ConstructorBuilder(cInfo, InjectorUtils.FromMethod(cInfo));
                        }
                        continue;

                    default:
                        continue;
                    }

                    if (builderMember != null)
                    {
                        if (builderMembers == null)
                        {
                            builderMembers = new List <IBuilderMember>();
                        }

                        builderMembers.Add(builderMember);
                    }
                }

                if (builderMembers != null)
                {
                    typeInfo.builderMembers = builderMembers.ToArray();
                }

                if (typeInfo.constructorBuilder == null)
                {
                    var cInfos = members.Where(o => o.MemberType == MemberTypes.Constructor).Select(o => (ConstructorInfo)o);
                    foreach (var cInfo in cInfos)
                    {
                        if (!ValidateInjectConstructor(cInfo))
                        {
                            continue;
                        }
                        if (cInfo.GetParameters().Length != 0)
                        {
                            continue;
                        }
                        typeInfo.constructorBuilder = new ConstructorBuilder(cInfo, InjectorUtils.FromMethod(cInfo));
                        break;
                    }

                    if (typeInfo.constructorBuilder == null)
                    {
                        foreach (var cInfo in cInfos)
                        {
                            if (!ValidateInjectConstructor(cInfo))
                            {
                                continue;
                            }
                            typeInfo.constructorBuilder = new ConstructorBuilder(cInfo, InjectorUtils.FromMethod(cInfo));
                            break;
                        }
                    }
                }

                UpdateTransparentProxy(typeInfo);


                return typeInfo;
            }));
        }