本文整理汇总了C#中System.Reflection.ConstructorInfo.GetCustomAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorInfo.GetCustomAttributes方法的具体用法?C# ConstructorInfo.GetCustomAttributes怎么用?C# ConstructorInfo.GetCustomAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.ConstructorInfo
的用法示例。
在下文中一共展示了ConstructorInfo.GetCustomAttributes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteCustomAttributes
private void WriteCustomAttributes(XmlWriter writer, ConstructorInfo constructorInfo)
{
try
{
WriteCustomAttributes(writer, constructorInfo.GetCustomAttributes(this.rep.DocumentInheritedAttributes), "");
}
catch (Exception e)
{
TraceErrorOutput("Error retrieving custom attributes for " + MemberID.GetMemberID(constructorInfo), e);
}
}
示例2: FormatConstructor
/// <summary>
/// Formats the constructor.
/// </summary>
/// <param name="constructor">The constructor.</param>
/// <param name="sw">The string writer.</param>
private static void FormatConstructor(ConstructorInfo constructor, StringWriter sw)
{
foreach (Attribute attribute in constructor.GetCustomAttributes(false))
{
FormatAttribute(sw, attribute);
}
sw.Write(constructor.DeclaringType.GetTypeInfo().Name);
sw.Write("(");
foreach (var parameterInfo in constructor.GetParameters())
{
foreach (Attribute attribute in parameterInfo.GetCustomAttributes(false))
{
FormatAttribute(sw, attribute);
}
sw.Write(parameterInfo.ParameterType.Format());
sw.Write(" ");
sw.Write(parameterInfo.Name);
}
sw.WriteLine(")");
}
示例3: IsConstructable
public static bool IsConstructable( ConstructorInfo ctor, AccessLevel accessLevel )
{
object[] attrs = ctor.GetCustomAttributes( m_ConstructableType, false );
if ( attrs.Length == 0 )
return false;
return accessLevel >= ((ConstructableAttribute)attrs[0]).AccessLevel;
}
示例4: HasInjectAttribute
private bool HasInjectAttribute(ConstructorInfo constructor)
{
return constructor.GetCustomAttributes(false).Any(a => a.GetType().Name.ToLower().Contains("inject"));
}
示例5: GetConstructorExpression
public Expression GetConstructorExpression(out ConstructorInfo constructorInfo, out IEnumerable<Expression> constructorParameters)
{
CompiledExportDelegateInfo exportDelegateInfo = _instanceCompiledExport.exportDelegateInfo;
constructorInfo = _instanceCompiledExport.exportDelegateInfo.ImportConstructor ??
_instanceCompiledExport.PickConstructor(exportDelegateInfo.ActivationType);
if (constructorInfo == null)
{
throw new PublicConstructorNotFoundException(exportDelegateInfo.ActivationType);
}
List<Expression> parameters = new List<Expression>();
constructorParameters = parameters;
Attribute[] constructorAttributes = constructorInfo.GetCustomAttributes(true).ToArray();
if (constructorAttributes.Length == 0)
{
constructorAttributes = EmptyAttributesArray;
}
foreach (ParameterInfo parameterInfo in constructorInfo.GetParameters())
{
Attribute[] parameterAttributes = parameterInfo.GetCustomAttributes(true).ToArray();
if (parameterAttributes.Length == 0)
{
parameterAttributes = EmptyAttributesArray;
}
IExportValueProvider valueProvider = null;
ExportStrategyFilter exportStrategyFilter = null;
string importName = null;
object comparerObject = null;
ILocateKeyValueProvider locateKey = null;
if (exportDelegateInfo.ConstructorParams != null)
{
foreach (ConstructorParamInfo constructorParamInfo in exportDelegateInfo.ConstructorParams)
{
if (string.Compare(parameterInfo.Name,
constructorParamInfo.ParameterName,
StringComparison.OrdinalIgnoreCase) == 0)
{
importName = constructorParamInfo.ImportName;
exportStrategyFilter = constructorParamInfo.ExportStrategyFilter;
valueProvider = constructorParamInfo.ValueProvider;
comparerObject = constructorParamInfo.ComparerObject;
locateKey = constructorParamInfo.LocateKeyProvider;
break;
}
}
if (valueProvider == null)
{
foreach (ConstructorParamInfo constructorParamInfo in exportDelegateInfo.ConstructorParams)
{
if (string.IsNullOrEmpty(constructorParamInfo.ParameterName) &&
parameterInfo.ParameterType.GetTypeInfo().IsAssignableFrom(
constructorParamInfo.ParameterType.GetTypeInfo()))
{
importName = constructorParamInfo.ImportName;
exportStrategyFilter = constructorParamInfo.ExportStrategyFilter;
valueProvider = constructorParamInfo.ValueProvider;
comparerObject = constructorParamInfo.ComparerObject;
locateKey = constructorParamInfo.LocateKeyProvider;
break;
}
}
}
}
InjectionTargetInfo targetInfo = null;
if (importName != null)
{
targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
_instanceCompiledExport.activationTypeAttributes,
parameterInfo,
parameterAttributes,
constructorAttributes,
importName,
null);
}
else if (InjectionKernel.ImportTypeByName(parameterInfo.ParameterType))
{
targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
_instanceCompiledExport.activationTypeAttributes,
parameterInfo,
parameterAttributes,
constructorAttributes,
parameterInfo.Name,
null);
}
else
{
targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
_instanceCompiledExport.activationTypeAttributes,
parameterInfo,
//.........这里部分代码省略.........
示例6: Constructor
public Constructor(ConstructorInfo constructorInfo, Dictionary<string, object> parameterDictionary)
{
_constructorInfo = constructorInfo;
_parameterNames = new List<string>();
_parameterTypes = new List<Type>();
_match = true;
_usedAllParameters = true;
// look through the constructor attributes for a RegisterConstructorAttribute
// if this attribute exists, the factory is allowed to use the constructor.
var registerConstructorAttributes = (RegisterConstructorAttribute[])_constructorInfo.GetCustomAttributes(typeof(RegisterConstructorAttribute), false);
if (registerConstructorAttributes.Length == 0)
{
_match = false;
_usedAllParameters = false;
return;
}
// populate _parameterNames and _parameterTypes from _constructorInfo
ParameterInfo[] parameterInfos = _constructorInfo.GetParameters();
foreach (ParameterInfo parameterInfo in parameterInfos)
{
var factoryParameterAttributes = (FactoryParameterAttribute[])parameterInfo.GetCustomAttributes(typeof(FactoryParameterAttribute),false);
string parameterName = parameterInfo.Name.ToLower(); // use raw parameter name
if (factoryParameterAttributes.Length == 1) parameterName = factoryParameterAttributes[0].RegisteredName; // replace with friendly name for the parameter.
else if (factoryParameterAttributes.Length > 1) throw new Exception("Parameter can have at most one FactoryParameterAttribute");
_parameterNames.Add(parameterName);
_parameterTypes.Add(parameterInfo.ParameterType);
}
// populate _parameterValues from parameterDictionary
_parameterValues = new object[_parameterNames.Count];
for (int i = 0; i < _parameterNames.Count; ++i)
{
string parameterName = _parameterNames[i].ToLower();
Type parameterType = _parameterTypes[i];
object parameterValue;
if (parameterDictionary.ContainsKey(parameterName))
{
parameterValue = parameterDictionary[parameterName];
if (!parameterType.IsInstanceOfType(parameterValue)) // Parameter parameterName has the wrong type
{
if (parameterValue.GetType() == typeof(Dictionary<string, object>))
{
var dictionary = (Dictionary<string, object>) parameterValue;
MethodInfo factoryGetInstanceMethod = typeof(Factory).GetMethod("GetInstance").MakeGenericMethod(parameterType);
try
{
parameterValue = factoryGetInstanceMethod.Invoke(null, new object[] { dictionary });
}
catch (TargetInvocationException e)
{
throw new Exception(e.InnerException.Message);
}
}
else
_match = false;
}
}
else // Can't find parameter parameterName in parameterDictionary
{
_match = false;
parameterValue = new object();
}
_parameterValues[i] = parameterValue;
}
// check to see whether all parameters in parameterDictionary have been used up
foreach (KeyValuePair<string, object> entry in parameterDictionary.Where(entry => entry.Key != "name" && !_parameterNames.Contains(entry.Key)))
_usedAllParameters = false;
}
示例7: GetCAL
public static CAL GetCAL(ConstructorInfo ctor)
{
object[] attrs = ctor.GetCustomAttributes(m_ConstructableAccessLevel, false);
if (attrs.Length == 0)
return null;
return attrs[0] as CAL;
}
示例8: PublicConstructor_IsTaggedWithPublicAPIAttribute
public void PublicConstructor_IsTaggedWithPublicAPIAttribute(ConstructorInfo constructor)
{
bool attributeIsPresent = constructor.GetCustomAttributes(typeof(PublicAPIAttribute), true).Any();
Assume.That(constructor.DeclaringType != null);
Assert.That(attributeIsPresent, "Constructor (" + string.Join(", ", constructor.GetParameters().Select(p => p.ParameterType.Name)) + " of type " + constructor.DeclaringType.Name + " is not tagged with [PublicAPI]");
}
示例9: GetAttribute
public static ExcelObjectConstructorAttribute GetAttribute(ConstructorInfo ci)
{
object[] o = ci.GetCustomAttributes(typeof(ExcelObjectConstructorAttribute), false);
ExcelObjectConstructorAttribute r = (ExcelObjectConstructorAttribute)o[0];
return r;
}
示例10: BuildConstructorDefinition
private static ConstructorDefinition BuildConstructorDefinition(ConstructorInfo constructorInfo)
{
IEnumerable<ParameterDefinition> parameters = constructorInfo
.GetParameters()
.Select(p => new ParameterDefinition(p.Name, p.ParameterType));
ConstructorMethod constructorMethod = ObjectInterfaceProvider.GetConstructor(constructorInfo);
bool isPreferredConstructor = constructorInfo.GetCustomAttributes(typeof(SerializationConstructorAttribute), true).Any();
return new ConstructorDefinition(constructorMethod, parameters, isPreferredConstructor);
}