本文整理汇总了C#中Compilation.GetTypeByMetadataName方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.GetTypeByMetadataName方法的具体用法?C# Compilation.GetTypeByMetadataName怎么用?C# Compilation.GetTypeByMetadataName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Compilation
的用法示例。
在下文中一共展示了Compilation.GetTypeByMetadataName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateAnalyzerWithinCompilation
public IDiagnosticAnalyzer CreateAnalyzerWithinCompilation(Compilation compilation, AnalyzerOptions options, CancellationToken cancellationToken)
{
var dllImportType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.DllImportAttribute");
if (dllImportType == null)
{
return null;
}
var marshalAsType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.MarshalAsAttribute");
if (marshalAsType == null)
{
return null;
}
var stringBuilderType = compilation.GetTypeByMetadataName("System.Text.StringBuilder");
if (stringBuilderType == null)
{
return null;
}
var unmanagedType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.UnmanagedType");
if (unmanagedType == null)
{
return null;
}
return new Analyzer(dllImportType, marshalAsType, stringBuilderType, unmanagedType);
}
示例2: ShouldOmitThisDiagnostic
private static bool ShouldOmitThisDiagnostic(ISymbol symbol, Compilation compilation)
{
// This diagnostic is only relevant in constructors.
// TODO: should this apply to instance field initializers for VB?
var m = symbol as IMethodSymbol;
if (m == null || m.MethodKind != MethodKind.Constructor)
{
return true;
}
var containingType = m.ContainingType;
if (containingType == null)
{
return true;
}
// special case ASP.NET and WinForms constructors
INamedTypeSymbol webUiControlType = compilation.GetTypeByMetadataName("System.Web.UI.Control");
if (containingType.Inherits(webUiControlType))
{
return true;
}
INamedTypeSymbol windowsFormsControlType = compilation.GetTypeByMetadataName("System.Windows.Forms.Control");
if (containingType.Inherits(windowsFormsControlType))
{
return true;
}
return false;
}
示例3: CreateAnalyzerWithinCompilation
public IDiagnosticAnalyzer CreateAnalyzerWithinCompilation(Compilation compilation, AnalyzerOptions options, CancellationToken cancellationToken)
{
var specializedCollectionsSymbol = compilation.GetTypeByMetadataName(SpecializedCollectionsMetadataName);
if (specializedCollectionsSymbol == null)
{
// TODO: In the future, we may want to run this analyzer even if the SpecializedCollections
// type cannot be found in this compilation. In some cases, we may want to add a reference
// to SpecializedCollections as a linked file or an assembly that contains it. With this
// check, we will not warn where SpecializedCollections is not yet referenced.
return null;
}
var genericEnumerableSymbol = compilation.GetTypeByMetadataName(IEnumerableMetadataName);
if (genericEnumerableSymbol == null)
{
return null;
}
var linqEnumerableSymbol = compilation.GetTypeByMetadataName(LinqEnumerableMetadataName);
if (linqEnumerableSymbol == null)
{
return null;
}
var genericEmptyEnumerableSymbol = linqEnumerableSymbol.GetMembers(EmptyMethodName).FirstOrDefault() as IMethodSymbol;
if (genericEmptyEnumerableSymbol == null ||
genericEmptyEnumerableSymbol.Arity != 1 ||
genericEmptyEnumerableSymbol.Parameters.Length != 0)
{
return null;
}
return GetCodeBlockStartedAnalyzer(genericEnumerableSymbol, genericEmptyEnumerableSymbol);
}
示例4: OnCompilationStarted
public ICompilationEndedAnalyzer OnCompilationStarted(Compilation compilation, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
{
var iserializableTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.ISerializable");
if (iserializableTypeSymbol == null)
{
return null;
}
var serializationInfoTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.SerializationInfo");
if (serializationInfoTypeSymbol == null)
{
return null;
}
var streamingContextTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.StreamingContext");
if (streamingContextTypeSymbol == null)
{
return null;
}
var serializableAttributeTypeSymbol = compilation.GetTypeByMetadataName("System.SerializableAttribute");
if (serializableAttributeTypeSymbol == null)
{
return null;
}
return new Analyzer(iserializableTypeSymbol, serializationInfoTypeSymbol, streamingContextTypeSymbol, serializableAttributeTypeSymbol);
}
示例5: CreateAnalyzerWithinCompilation
public IDiagnosticAnalyzer CreateAnalyzerWithinCompilation(Compilation compilation, AnalyzerOptions options, CancellationToken cancellationToken)
{
var iserializableTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.ISerializable");
if (iserializableTypeSymbol == null)
{
return null;
}
var serializationInfoTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.SerializationInfo");
if (serializationInfoTypeSymbol == null)
{
return null;
}
var streamingContextTypeSymbol = compilation.GetTypeByMetadataName("System.Runtime.Serialization.StreamingContext");
if (streamingContextTypeSymbol == null)
{
return null;
}
var serializableAttributeTypeSymbol = compilation.GetTypeByMetadataName("System.SerializableAttribute");
if (serializableAttributeTypeSymbol == null)
{
return null;
}
return new Analyzer(iserializableTypeSymbol, serializationInfoTypeSymbol, streamingContextTypeSymbol, serializableAttributeTypeSymbol);
}
示例6: OnCompilationStarted
public ICompilationEndedAnalyzer OnCompilationStarted(Compilation compilation, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
{
var dllImportType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.DllImportAttribute");
if (dllImportType == null)
{
return null;
}
var marshalAsType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.MarshalAsAttribute");
if (marshalAsType == null)
{
return null;
}
var stringBuilderType = compilation.GetTypeByMetadataName("System.Text.StringBuilder");
if (stringBuilderType == null)
{
return null;
}
var unmanagedType = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.UnmanagedType");
if (unmanagedType == null)
{
return null;
}
return new Analyzer(dllImportType, marshalAsType, stringBuilderType, unmanagedType);
}
示例7: GetTaskTypes
private static ImmutableArray<INamedTypeSymbol> GetTaskTypes(Compilation compilation)
{
INamedTypeSymbol taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
INamedTypeSymbol taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
return ImmutableArray.Create(taskType, taskOfTType);
}
示例8: AnalyzeSymbol
public void AnalyzeSymbol(ISymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
{
var methodSymbol = (IMethodSymbol)symbol;
var httpControllerInterfaceSymbol = compilation.GetTypeByMetadataName("System.Web.Http.Controllers.IHttpController");
if (methodSymbol.ContainingType.AllInterfaces.Any(x => x.MetadataName == httpControllerInterfaceSymbol.MetadataName))
{
var postAttributeSymbol = compilation.GetTypeByMetadataName("System.Web.Http.HttpPostAttribute");
if (methodSymbol.ReturnsVoid && (methodSymbol.MetadataName.ToLowerInvariant().StartsWith("post") || methodSymbol.GetAttributes().Any(x => x.AttributeClass.MetadataName == postAttributeSymbol.MetadataName)))
{
var diagnostic = Diagnostic.Create(Rule, methodSymbol.Locations[0], methodSymbol.Name);
addDiagnostic(diagnostic);
}
}
}
示例9: TypeHasWeakIdentity
private bool TypeHasWeakIdentity(ITypeSymbol type, Compilation compilation)
{
switch (type.TypeKind)
{
case TypeKind.Array:
var arrayType = type as IArrayTypeSymbol;
return arrayType != null && IsPrimitiveType(arrayType.ElementType);
case TypeKind.Class:
case TypeKind.TypeParameter:
INamedTypeSymbol marshalByRefObjectTypeSymbol = compilation.GetTypeByMetadataName("System.MarshalByRefObject");
INamedTypeSymbol executionEngineExceptionTypeSymbol = compilation.GetTypeByMetadataName("System.ExecutionEngineException");
INamedTypeSymbol outOfMemoryExceptionTypeSymbol = compilation.GetTypeByMetadataName("System.OutOfMemoryException");
INamedTypeSymbol stackOverflowExceptionTypeSymbol = compilation.GetTypeByMetadataName("System.StackOverflowException");
INamedTypeSymbol memberInfoTypeSymbol = compilation.GetTypeByMetadataName("System.Reflection.MemberInfo");
INamedTypeSymbol parameterInfoTypeSymbol = compilation.GetTypeByMetadataName("System.Reflection.ParameterInfo");
INamedTypeSymbol threadTypeSymbol = compilation.GetTypeByMetadataName("System.Threading.Thread");
return
type.SpecialType == SpecialType.System_String ||
type.Equals(executionEngineExceptionTypeSymbol) ||
type.Equals(outOfMemoryExceptionTypeSymbol) ||
type.Equals(stackOverflowExceptionTypeSymbol) ||
type.Inherits(marshalByRefObjectTypeSymbol) ||
type.Inherits(memberInfoTypeSymbol) ||
type.Inherits(parameterInfoTypeSymbol) ||
type.Inherits(threadTypeSymbol);
// What about struct types?
default:
return false;
}
}
示例10: AnalyzeSymbol
public void AnalyzeSymbol(ISymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
{
// Filter out methods that do not use Async and make sure to include methods that return a Task
var methodSymbol = (IMethodSymbol)symbol;
// Check if method name is an override or virtual class. If it is disregard it.
// (This assumes if a method is virtual the programmer will not want to change the name)
// Check if the method returns a Task or Task<TResult>
if ((methodSymbol.ReturnType == compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")
|| methodSymbol.ReturnType.OriginalDefinition == compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1").OriginalDefinition)
&& !methodSymbol.Name.EndsWith("Async") && !methodSymbol.IsOverride && !methodSymbol.IsVirtual)
{
addDiagnostic(Diagnostic.Create(RenameAsyncMethod, methodSymbol.Locations[0], methodSymbol.Name));
return;
}
return;
}
示例11: AnalyzeSymbol
public void AnalyzeSymbol(ISymbol symbol, Compilation compilation, Action<Diagnostic> addDiagnostic, AnalyzerOptions options, CancellationToken cancellationToken)
{
// Filter out methods that do not use Async and that do not have exactly two parameters
var methodSymbol = (IMethodSymbol)symbol;
var eventType = compilation.GetTypeByMetadataName("System.EventArgs");
if (methodSymbol.ReturnsVoid && methodSymbol.IsAsync)
{
if (methodSymbol.Parameters.Length == 2)
{
var firstParam = methodSymbol.Parameters[0];
var secondParam = methodSymbol.Parameters[1];
if (firstParam is object)
{
// Check each parameter for EventHandler shape and return if it matches.
if (firstParam.Name.ToLower().Equals("sender") && secondParam.Type == eventType)
{
return;
}
else
{
// Check if the second parameter implements EventArgs. If it does; return.
var checkForEventType = secondParam.Type.BaseType;
while (checkForEventType.OriginalDefinition != compilation.GetTypeByMetadataName("System.Object"))
{
if (checkForEventType == eventType)
{
return;
}
checkForEventType = checkForEventType.BaseType;
}
}
}
}
addDiagnostic(Diagnostic.Create(VoidReturnType, methodSymbol.Locations[0], methodSymbol.Name));
return;
}
return;
}
示例12: EventSourceGenerator
private EventSourceGenerator(Document document, Compilation compilation, SyntaxTree syntaxTree, CompilationUnitSyntax root, SemanticModel semanticModel, FrameworkName targetFramework)
{
if (document == null)
throw new ArgumentNullException("document", "document is null.");
if (compilation == null)
throw new ArgumentNullException("compilation", "compilation is null.");
if (syntaxTree == null)
throw new ArgumentNullException("syntaxTree", "syntaxTree is null.");
if (root == null)
throw new ArgumentNullException("root", "root is null.");
if (semanticModel == null)
throw new ArgumentNullException("semanticModel", "semanticModel is null.");
m_targetFramework = targetFramework;
m_document = document;
m_compilation = compilation;
m_syntaxTree = syntaxTree;
m_root = root;
m_semanticModel = semanticModel;
EventSourceTypes = ImmutableList.CreateRange(new[]
{
m_compilation.GetTypeByMetadataName("System.Diagnostics.Tracing.EventSource"),
m_compilation.GetTypeByMetadataName("Microsoft.Diagnostics.Tracing.EventSource")
}
.Where(s => s != null)
.Select(s => new EventSourceTypeInfo(m_semanticModel, s)));
if (EventSourceTypes.Count == 0)
throw new CodeGeneratorException("The class System.Diagnostics.Tracing.EventSource or Microsoft.Diagnostics.Tracing.EventSource could not be found. You need to add a reference to the assembly containing one of these classes to continue.");
m_generator = SyntaxGenerator.GetGenerator(document);
m_parameterConverters = new ParameterConverterCollection(m_semanticModel, m_generator);
}
示例13: OnCompilationStarted
public ICompilationEndedAnalyzer OnCompilationStarted(Compilation compilation, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
{
var genericEnumerableSymbol = compilation.GetTypeByMetadataName(IEnumerableMetadataName);
if (genericEnumerableSymbol == null)
{
return null;
}
var linqEnumerableSymbol = compilation.GetTypeByMetadataName(LinqEnumerableMetadataName);
if (linqEnumerableSymbol == null)
{
return null;
}
var genericEmptyEnumerableSymbol = linqEnumerableSymbol.GetMembers(EmptyMethodName).FirstOrDefault() as IMethodSymbol;
if (genericEmptyEnumerableSymbol == null ||
genericEmptyEnumerableSymbol.Arity != 1 ||
genericEmptyEnumerableSymbol.Parameters.Length != 0)
{
return null;
}
return GetCodeBlockStartedAnalyzer(genericEnumerableSymbol, genericEmptyEnumerableSymbol);
}
示例14: GetTypeByName
public static INamedTypeSymbol GetTypeByName(string className, Compilation compilation)
{
var type = compilation.GetTypeByMetadataName(className);
// Another way to access the info
if (type == null)
{
Func<string, bool> pred = s => { return s.Equals(className); };
var symbols = compilation.GetSymbolsWithName(pred, SymbolFilter.Type);
if (symbols.Count() > 0)
{
type = symbols.First() as INamedTypeSymbol;
}
}
return type;
}
示例15: GetSpecialTypeLibAttributeConstructorsWorker
/// <summary>
/// The TypeLib*Attribute classes that accept TypeLib*Flags with FHidden as an option all have two constructors,
/// one accepting a TypeLib*Flags and the other a short. This methods gets those two constructor symbols for any
/// of these attribute classes. It does not require that the either of these types be those shipped by Microsoft,
/// but it does demand the types found follow the expected pattern. If at any point that pattern appears to be
/// violated, return an empty enumerable to indicate that no appropriate constructors were found.
/// </summary>
private static List<IMethodSymbol> GetSpecialTypeLibAttributeConstructorsWorker(
Compilation compilation,
string attributeMetadataName,
string flagsMetadataName)
{
var typeLibAttributeType = compilation.GetTypeByMetadataName(attributeMetadataName);
var typeLibFlagsType = compilation.GetTypeByMetadataName(flagsMetadataName);
var shortType = compilation.GetSpecialType(SpecialType.System_Int16);
if (typeLibAttributeType == null || typeLibFlagsType == null || shortType == null)
{
return new List<IMethodSymbol>();
}
var candidateConstructors = typeLibAttributeType.Constructors
.Where(c => c.Parameters.Length == 1 &&
(c.Parameters[0].Type == typeLibFlagsType || c.Parameters[0].Type == shortType));
candidateConstructors = candidateConstructors.Where(c => (!c.IsVararg &&
!c.Parameters[0].IsRefOrOut() &&
!c.Parameters[0].CustomModifiers.Any()));
return candidateConstructors.ToList();
}