在编写T4文本模板的过程中,我遇到了一个我正在努力解决的问题.我需要知道我正在处理的枚举的类型.
我有基于byte和的枚举ushort.我需要T4文本模板来编写代码以将枚举转换为正确的值类型,以便序列化枚举并将其放入字节数组中.
这是byte类型的示例枚举
namespace CodeEnumType
{
public enum MyEnum : byte
{
Member1 = 0,
Member2 = 1,
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的T4文本模板
<#@ template hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="EnvDte" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.Collections.Generic" #>
<#
var serviceProvider = this.Host as IServiceProvider;
var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
var project = dte.Solution.FindProjectItem(this.Host.TemplateFile).ContainingProject as Project;
var projectItems = GetProjectItemsRecursively(project.ProjectItems);
foreach(var projectItem in projectItems)
{
var fileCodeModel = projectItem.FileCodeModel;
if(fileCodeModel == …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个代码分析器,用于查找Visual Studio 2015解决方案中未从任何其他类型引用的类型.
我的问题是我无法弄清楚如何找到未引用类型的列表.
我已经尝试过DOM,你可以从下面的代码中看到,但我不知道在哪里导航,当前的代码似乎已经很慢了.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;
namespace AlphaSolutions.CodeAnalysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ZeroReferencesDiagnosticAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "ZeroReferences";
private static DiagnosticDescriptor rule = new DiagnosticDescriptor(
DiagnosticId,
title: "Type has zero code references",
messageFormat: "Type '{0}' is not referenced within the solution",
category: "Naming",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "Type should have references."
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
return ImmutableArray.Create(rule);
}
} …Run Code Online (Sandbox Code Playgroud)