// Order so that the UseWhen==null case comes last. public int CompareTo(object obj) { ConstructionAttribute ca = obj as ConstructionAttribute; if (ca == null) { return(-1); } if ((UseWhen == null) && (ca.UseWhen != null)) { return(1); } if ((UseWhen != null) && (ca.UseWhen == null)) { return(-1); } return(0); }
public static List <ConstructionAttribute> GetConstructionAttribute(Type type) { lock (cache) { if (!cache.ContainsKey(type)) { var cas = new List <ConstructionAttribute>(); // Look for constructors foreach (ConstructorInfo ci in type.GetConstructors()) { if (!ci.IsDefined(typeof(ConstructionAttribute), true)) { continue; } ConstructionAttribute ca = ci.GetCustomAttributes(typeof(ConstructionAttribute), true)[0] as ConstructionAttribute; ca.TargetMember = ci; cas.Add(ca); } // Look for factory methods foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy)) { if (!mi.IsDefined(typeof(ConstructionAttribute), true)) { continue; } if (mi.ReturnType != type) { continue; } ConstructionAttribute ca = mi.GetCustomAttributes(typeof(ConstructionAttribute), true)[0] as ConstructionAttribute; ca.TargetMember = mi; cas.Add(ca); } // Order so that the UseWhen==null case comes last. // Any other priorities could be included in here at some point if needed. cas.Sort(); cache[type] = cas; } return(cache[type]); } }