private void ScanType(Type type) { var attribute = SRReflection.GetAttribute <ServiceAttribute>(type); if (attribute != null) { _serviceStubs.Add(new ServiceStub { Type = type, InterfaceType = attribute.ServiceType }); } ScanTypeForConstructors(type, _serviceStubs); ScanTypeForSelectors(type, _serviceStubs); }
private static List <FieldInfo> ScanType(Type t) { var cache = new List <FieldInfo>(); // Check for attribute added to the class var globalAttr = SRReflection.GetAttribute <RequiredFieldAttribute>(t); #if NETFX_CORE var fields = t.GetTypeInfo().DeclaredFields.Where(f => !f.IsStatic); #else // Check each field for the attribute var fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); #endif foreach (var f in fields) { var requiredFieldAttribute = SRReflection.GetAttribute <RequiredFieldAttribute>(f); var importAttribute = SRReflection.GetAttribute <ImportAttribute>(f); if (globalAttr == null && requiredFieldAttribute == null && importAttribute == null) { continue; // Early out if no attributes found. } var info = new FieldInfo(); info.Field = f; if (importAttribute != null) { info.Import = true; info.ImportType = importAttribute.Service; } else if (requiredFieldAttribute != null) { info.AutoSet = requiredFieldAttribute.AutoSearch; info.AutoCreate = requiredFieldAttribute.AutoCreate; } else { info.AutoSet = globalAttr.AutoSearch; info.AutoCreate = globalAttr.AutoCreate; } cache.Add(info); } return(cache); }
private static void ScanTypeForSelectors(Type t, List <ServiceStub> stubs) { var methods = GetStaticMethods(t); foreach (var method in methods) { var attrib = SRReflection.GetAttribute <ServiceSelectorAttribute>(method); if (attrib == null) { continue; } if (method.ReturnType != typeof(Type)) { Debug.LogError("ServiceSelector must have return type of Type ({0}.{1}())".Fmt(t.Name, method.Name)); continue; } if (method.GetParameters().Length > 0) { Debug.LogError("ServiceSelector must have no parameters ({0}.{1}())".Fmt(t.Name, method.Name)); continue; } var stub = stubs.FirstOrDefault(p => p.InterfaceType == attrib.ServiceType); if (stub == null) { stub = new ServiceStub { InterfaceType = attrib.ServiceType }; stubs.Add(stub); } #if NETFX_CORE stub.Selector = (Func <Type>)method.CreateDelegate(typeof(Func <Type>)); #else stub.Selector = (Func <Type>)Delegate.CreateDelegate(typeof(Func <Type>), method); #endif } }
private static void ScanTypeForConstructors(Type t, List <ServiceStub> stubs) { var methods = GetStaticMethods(t); foreach (var method in methods) { var attrib = SRReflection.GetAttribute <ServiceConstructorAttribute>(method); if (attrib == null) { continue; } if (method.ReturnType != attrib.ServiceType) { Debug.LogError("ServiceConstructor must have return type of {2} ({0}.{1}())".Fmt(t.Name, method.Name, attrib.ServiceType)); continue; } if (method.GetParameters().Length > 0) { Debug.LogError("ServiceConstructor must have no parameters ({0}.{1}())".Fmt(t.Name, method.Name)); continue; } var stub = stubs.FirstOrDefault(p => p.InterfaceType == attrib.ServiceType); if (stub == null) { stub = new ServiceStub { InterfaceType = attrib.ServiceType }; stubs.Add(stub); } var m = method; stub.Constructor = () => m.Invoke(null, null); } }
/// <summary> /// Scan <paramref name="obj" /> for valid options and return a collection of them. /// </summary> /// <param name="obj"></param> /// <returns></returns> public static ICollection <OptionDefinition> ScanForOptions(object obj) { var options = new List <OptionDefinition>(); #if NETFX_CORE var members = obj.GetType().GetTypeInfo().DeclaredMembers; #else var members = obj.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.InvokeMethod); #endif foreach (var memberInfo in members) { // Find user-specified category name from attribute var categoryAttribute = SRReflection.GetAttribute <CategoryAttribute>(memberInfo); var category = categoryAttribute == null ? "Default" : categoryAttribute.Category; // Find user-specified sorting priority from attribute var sortAttribute = SRReflection.GetAttribute <SortAttribute>(memberInfo); var sortPriority = sortAttribute == null ? 0 : sortAttribute.SortPriority; // Find user-specified display name from attribute var nameAttribute = SRReflection.GetAttribute <DisplayNameAttribute>(memberInfo); var name = nameAttribute == null ? memberInfo.Name : nameAttribute.DisplayName; if (memberInfo is PropertyInfo) { var propertyInfo = memberInfo as PropertyInfo; // Only allow properties with public read/write #if NETFX_CORE if (propertyInfo.GetMethod == null) { continue; } // Ignore static members if (propertyInfo.GetMethod.IsStatic) { continue; } #else if (propertyInfo.GetGetMethod() == null) { continue; } // Ignore static members if ((propertyInfo.GetGetMethod().Attributes & MethodAttributes.Static) != 0) { continue; } #endif options.Add(new OptionDefinition(name, category, sortPriority, new SRF.Helpers.PropertyReference(obj, propertyInfo))); } else if (memberInfo is MethodInfo) { var methodInfo = memberInfo as MethodInfo; if (methodInfo.IsStatic) { continue; } // Skip methods with parameters or non-void return type if (methodInfo.ReturnType != typeof(void) || methodInfo.GetParameters().Length > 0) { continue; } options.Add(new OptionDefinition(name, category, sortPriority, new SRF.Helpers.MethodReference(obj, methodInfo))); } } return(options); }