public static CustomAttributeBuilder CreateCustomAttribute( Type type, object[] ctorArgs, Attribute sourceAttribute) { #region Sanity Checks AssertUtils.ArgumentNotNull(type, "type"); if (!typeof(Attribute).IsAssignableFrom(type)) { throw new ArgumentException( string.Format("[{0}] does not derive from the [System.Attribute] class.", type.FullName)); } #endregion ConstructorInfo ci = type.GetConstructor(ReflectionUtils.GetTypes(ctorArgs)); if (ci == null && ctorArgs.Length == 0) { ci = type.GetConstructors()[0]; ctorArgs = GetDefaultValues(GetParameterTypes(ci.GetParameters())); } if (sourceAttribute != null) { object defaultAttribute = null; try { defaultAttribute = ci.Invoke(ctorArgs); } catch { } IList <PropertyInfo> getSetProps = new List <PropertyInfo>(); IList getSetValues = new ArrayList(); IList <PropertyInfo> readOnlyProps = new List <PropertyInfo>(); IList readOnlyValues = new ArrayList(); foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (pi.DeclaringType == typeof(Attribute)) { continue; } if (pi.CanRead) { if (pi.CanWrite) { object propValue = pi.GetValue(sourceAttribute, null); if (defaultAttribute != null) { object defaultValue = pi.GetValue(defaultAttribute, null); if ((propValue == null && defaultValue == null) || (propValue != null && propValue.Equals(defaultValue))) { continue; } } getSetProps.Add(pi); getSetValues.Add(propValue); } else { readOnlyProps.Add(pi); readOnlyValues.Add(pi.GetValue(sourceAttribute, null)); } } } if (readOnlyProps.Count == 1) { PropertyInfo pi = readOnlyProps[0]; ConstructorInfo ciTemp = type.GetConstructor(new Type[1] { pi.PropertyType }); if (ciTemp != null) { ci = ciTemp; ctorArgs = new object[1] { readOnlyValues[0] }; } else { ciTemp = type.GetConstructor(new Type[1] { readOnlyValues[0].GetType() }); if (ciTemp != null) { ci = ciTemp; ctorArgs = new object[1] { readOnlyValues[0] }; } } } PropertyInfo[] propertyInfos = new PropertyInfo[getSetProps.Count]; getSetProps.CopyTo(propertyInfos, 0); object[] propertyValues = new object[getSetValues.Count]; getSetValues.CopyTo(propertyValues, 0); return(new CustomAttributeBuilder(ci, ctorArgs, propertyInfos, propertyValues)); } else { return(new CustomAttributeBuilder(ci, ctorArgs)); } }
public static Type[] GetParameterTypes(MethodBase method) { AssertUtils.ArgumentNotNull(method, "method"); return(GetParameterTypes(method.GetParameters())); }
public static string[] GetGenericParameterNames(MethodInfo method) { AssertUtils.ArgumentNotNull(method, "method"); return(GetGenericParameterNames(method.GetGenericArguments())); }
public static string[] Split( string s, string delimiters, bool trimTokens, bool ignoreEmptyTokens, string quoteChars) { if (s == null) { return(new string[0]); } if (string.IsNullOrEmpty(delimiters)) { return(new string[] { s }); } if (quoteChars == null) { quoteChars = string.Empty; } AssertUtils.IsTrue(quoteChars.Length % 2 == 0, "the number of quote characters must be even"); char[] delimiterChars = delimiters.ToCharArray(); // ɨÃè·Ö¸î·ûλÖà int[] delimiterPositions = new int[s.Length]; int count = MakeDelimiterPositionList(s, delimiterChars, quoteChars, delimiterPositions); List <string> tokens = new List <string>(count + 1); int startIndex = 0; for (int ixSep = 0; ixSep < count; ixSep++) { string token = s.Substring(startIndex, delimiterPositions[ixSep] - startIndex); if (trimTokens) { token = token.Trim(); } if (!(ignoreEmptyTokens && token.Length == 0)) { tokens.Add(token); } startIndex = delimiterPositions[ixSep] + 1; } if (startIndex < s.Length) { string token = s.Substring(startIndex); if (trimTokens) { token = token.Trim(); } if (!(ignoreEmptyTokens && token.Length == 0)) { tokens.Add(token); } } else if (startIndex == s.Length) { if (!(ignoreEmptyTokens)) { tokens.Add(string.Empty); } } return(tokens.ToArray()); }