本文整理汇总了C#中ITypeSymbol.GetAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# ITypeSymbol.GetAttributes方法的具体用法?C# ITypeSymbol.GetAttributes怎么用?C# ITypeSymbol.GetAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITypeSymbol
的用法示例。
在下文中一共展示了ITypeSymbol.GetAttributes方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HasStackOnlyAttribute
static bool HasStackOnlyAttribute(ITypeSymbol type)
{
foreach (var attribute in type.GetAttributes())
{
if (attribute.AttributeClass.Name == "StackOnlyAttribute")
{
return true;
}
}
return false;
}
示例2: AppendConstant
void AppendConstant (StringBuilder sb, ITypeSymbol constantType, object constantValue, bool useNumericalEnumValue = false)
{
if (constantValue is string) {
sb.Append (Highlight ("\"" + MonoDevelop.Ide.TypeSystem.Ambience.EscapeText ((string)constantValue) + "\"", colorStyle.String));
return;
}
if (constantValue is char) {
sb.Append (Highlight ("'" + constantValue + "'", colorStyle.String));
return;
}
if (constantValue is bool) {
sb.Append (Highlight ((bool)constantValue ? "true" : "false", colorStyle.KeywordConstants));
return;
}
if (constantValue == null) {
if (constantType.IsValueType) {
// structs can never be == null, therefore it's the default value.
sb.Append (Highlight ("default", colorStyle.KeywordSelection) + "(" + GetTypeReferenceString (constantType) + ")");
} else {
sb.Append (Highlight ("null", colorStyle.KeywordConstants));
}
return;
}
// TODOδ
// while (IsNullableType (constantType))
// constantType = NullableType.GetUnderlyingType (constantType);
if (constantType.TypeKind == TypeKind.Enum) {
foreach (var field in constantType.GetMembers ().OfType<IFieldSymbol> ()) {
if (field.ConstantValue == constantValue) {
if (useNumericalEnumValue) {
sb.Append (Highlight (string.Format ("0x{0:X}", field.ConstantValue), colorStyle.Number));
} else {
sb.Append (GetTypeReferenceString (constantType) + "." + FilterEntityName (field.Name));
}
return;
}
}
// try to decompose flags
if (constantType.GetAttributes ().Any (attr => attr.AttributeClass.Name == "FlagsAttribute" && attr.AttributeClass.ContainingNamespace.Name == "System")) {
var val = GetUlong (constantValue.ToString ());
var outVal = 0UL;
var fields = new List<IFieldSymbol> ();
foreach (var field in constantType.GetMembers ().OfType<IFieldSymbol> ()) {
if (field.ConstantValue == null)
continue;
var val2 = GetUlong (field.ConstantValue.ToString ());
if ((val & val2) == val2) {
fields.Add (field);
outVal |= val2;
}
}
if (val == outVal && fields.Count > 1) {
for (int i = 0; i < fields.Count; i++) {
if (i > 0)
sb.Append (" | ");
var field = fields [i];
sb.Append (GetTypeReferenceString (constantType) + "." + FilterEntityName (field.Name));
}
return;
}
}
sb.Append ("(" + GetTypeReferenceString (constantType) + ")" + Highlight (constantValue.ToString (), colorStyle.Number));
return;
}
sb.Append (Highlight (MonoDevelop.Ide.TypeSystem.Ambience.EscapeText (constantValue.ToString ()), colorStyle.Number));
}
示例3: IsSerializable
private bool IsSerializable(ITypeSymbol namedTypeSymbol)
{
return IsPrimitiveType(namedTypeSymbol) ||
namedTypeSymbol.SpecialType == SpecialType.System_String ||
namedTypeSymbol.SpecialType == SpecialType.System_Decimal ||
namedTypeSymbol.GetAttributes()
.Any(a => a.AttributeClass.Equals(_serializableAttributeTypeSymbol));
}
示例4: IsSerializable
private bool IsSerializable(ITypeSymbol namedTypeSymbol)
{
return namedTypeSymbol.GetAttributes().Any(a => a.AttributeClass == this.serializableAttributeTypeSymbol);
}
示例5: VerifyType
internal static void VerifyType(ContextReporter context, Location reportLocation, ITypeSymbol type, HashSet<ITypeSymbol> alreadyAnalyzed)
{
if (type.TypeKind == TypeKind.Array)
{
var array = type as IArrayTypeSymbol;
VerifyType(context, reportLocation, array.ElementType, alreadyAnalyzed);
return;
}
var namedTypeSymbol = (type as INamedTypeSymbol);
if (namedTypeSymbol != null && namedTypeSymbol.IsGenericType)
{
foreach (var item in namedTypeSymbol.TypeArguments)
{
VerifyType(context, reportLocation, item, alreadyAnalyzed);
}
return;
}
if (type.TypeKind == TypeKind.Class || type.TypeKind == TypeKind.Struct)
{
if (type.Locations[0].Kind == LocationKind.MetadataFile)
{
return;
}
if (!alreadyAnalyzed.Add(type)) return;
var typeLocation = type.Locations[0];
var members = type.GetMembers().Where(x => x is IFieldSymbol || x is IPropertySymbol)
.Where(x=> x.CanBeReferencedByName)
.ToArray();
if (members.Length != 0)
{
var hasDiagnostic = false;
foreach (var member in members)
{
if (!hasDiagnostic)
{
hasDiagnostic = VerifyMember(context, reportLocation, member, typeLocation);
}
VerifyType(context, reportLocation, (member as IFieldSymbol)?.Type ?? (member as IPropertySymbol)?.Type ?? null, alreadyAnalyzed);
}
if (!hasDiagnostic)
{
var dataContract = type.GetAttributes().FirstOrDefault(y => y.AttributeClass.ToString() == "System.Runtime.Serialization.DataContractAttribute");
if (dataContract == null)
{
context.ReportDiagnostic(Diagnostic.Create(SerializeTypeMustBeDataContract, reportLocation, new[] { typeLocation }, type.ToString()));
}
}
}
}
}
示例6: IsActualTypeException
private static bool IsActualTypeException(ITypeSymbol type)
{
if (type == null)
{
return false;
}
return type.GetAttributes().Any(att => att.AttributeClass.Name == "ExceptionalResponseTypeAttribute" && att.AttributeClass.ContainingNamespace.ToString() == "NCR.Engage.RoslynAnalysis");
}
示例7: IsFlagsEnum
/// <summary>
/// Check if the given type is an enum with System.FlagsAttribute.
/// </summary>
/// <remarks>
/// TODO: Can/should this be done using WellKnownAttributes?
/// </remarks>
private static bool IsFlagsEnum(ITypeSymbol typeSymbol)
{
Debug.Assert(typeSymbol != null);
if (typeSymbol.TypeKind != TypeKind.Enum)
{
return false;
}
foreach (var attribute in typeSymbol.GetAttributes())
{
var ctor = attribute.AttributeConstructor;
if (ctor != null)
{
var type = ctor.ContainingType;
if (!ctor.Parameters.Any() && type.Name == "FlagsAttribute")
{
var containingSymbol = type.ContainingSymbol;
if (containingSymbol.Kind == SymbolKind.Namespace &&
containingSymbol.Name == "System" &&
((INamespaceSymbol)containingSymbol.ContainingSymbol).IsGlobalNamespace)
{
return true;
}
}
}
}
return false;
}