Exemple #1
0
        public T Inject <T>(T @object)
        {
            if (@object == null)
            {
                throw new NullReferenceException("Null was given to Wire.inject.");
            }
            List <MemberInfo> injectionMembers = FindInjectionMembers(@object);
            List <MethodInfo> injectionMethods = FindInjectionMethods(@object);

            foreach (MemberInfo memberInfo in injectionMembers)
            {
                InjectAttribute injectAttribute;
                BindAttribute   bindAttribute;

                if (TypeHelper.TryToGetAttribute(memberInfo, out injectAttribute))
                {
                    if (memberInfo is FieldInfo)
                    {
                        (memberInfo as FieldInfo).SetValue(@object, this.Get(injectAttribute.name, (memberInfo as FieldInfo).FieldType));
                    }
                    else if (memberInfo is PropertyInfo)
                    {
                        (memberInfo as PropertyInfo).SetValue(@object, this.Get(injectAttribute.name, (memberInfo as PropertyInfo).PropertyType), null);
                    }
                }

                if (@object is MonoBehaviour && TypeHelper.TryToGetAttribute(memberInfo, out bindAttribute))
                {
                    Type   fieldType    = (memberInfo is FieldInfo) ? (memberInfo as FieldInfo).FieldType : (memberInfo as PropertyInfo).PropertyType;
                    object objectToBind = FindGameObjectByPath(@object as MonoBehaviour, bindAttribute.path);

                    if (fieldType.IsSubclassOf(typeof(Component)))
                    {
                        objectToBind = (objectToBind as GameObject).GetComponent(fieldType);
                    }

                    if (memberInfo is FieldInfo)
                    {
                        (memberInfo as FieldInfo).SetValue(@object, objectToBind);
                    }
                    else
                    {
                        (memberInfo as PropertyInfo).SetValue(@object, objectToBind, null);
                    }
                }
            }

            foreach (MethodInfo methodInfo in injectionMethods)
            {
                InvokationHelper.ResolveParametersAndInvokeMethod(this, methodInfo, @object);
            }

            return(@object);
        }
Exemple #2
0
        public override object GetInstance()
        {
            object instance = InvokationHelper.ResolveParametersAndInvokeMethod(wire, methodInfo, module);
            GameObjectAttribute gameObjectAttribute;

            if (instance is Type && TypeHelper.TryToGetAttribute(methodInfo, out gameObjectAttribute))
            {
                return(Instantiate((Type)instance, gameObjectAttribute));
            }
            else if (instance is UnityEngine.Object && TypeHelper.TryToGetAttribute(methodInfo, out gameObjectAttribute))
            {
                return(Instantiate((UnityEngine.Object)instance, gameObjectAttribute));
            }
            else
            {
                return(instance);
            }
        }
 public override object GetInstance()
 {
     object instance = InvokationHelper.ResolveParametersAndInvokeConstructor(wire, constructorInfo);
     return wire.Inject(instance);
 }