本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Refactoring.TypeSystemAstBuilder.ConvertType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeSystemAstBuilder.ConvertType方法的具体用法?C# TypeSystemAstBuilder.ConvertType怎么用?C# TypeSystemAstBuilder.ConvertType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.CSharp.Refactoring.TypeSystemAstBuilder
的用法示例。
在下文中一共展示了TypeSystemAstBuilder.ConvertType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitForeachStatement
public override void VisitForeachStatement(ForeachStatement foreachStatement)
{
base.VisitForeachStatement(foreachStatement);
var rr = ctx.Resolve(foreachStatement) as ForEachResolveResult;
if (rr == null)
return;
if (rr.ElementType.Kind == TypeKind.Unknown)
return;
if (ReflectionHelper.GetTypeCode(rr.ElementType) == TypeCode.Object)
return;
if (conversions == null) {
conversions = CSharpConversions.Get(ctx.Compilation);
}
Conversion c = conversions.ImplicitConversion(rr.ElementType, rr.ElementVariable.Type);
if (c.IsValid)
return;
var csResolver = ctx.GetResolverStateBefore(foreachStatement);
var builder = new TypeSystemAstBuilder(csResolver);
AstType elementType = builder.ConvertType(rr.ElementType);
AstType variableType = foreachStatement.VariableType;
string issueText = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'");
string fixText = ctx.TranslateString("Use type '{0}'");
AddIssue(variableType, string.Format(issueText, elementType.GetText(), variableType.GetText()),
new CodeAction(string.Format(fixText, elementType.GetText()),
script => script.Replace(variableType, elementType)));
}
示例2: EnumMemberCompletionData
public EnumMemberCompletionData(IType enumType, IEntity member, TypeSystemAstBuilder builder) : base(enumType.Name + "." + member.Name)
{
this.enumType = enumType;
this.member = member;
this.Image = ClassBrowserIconService.Const;
this.CompletionText = builder.ConvertType(enumType).ToString() + "." + member.Name;
}
示例3: GeneratePartialClassContextStub
static string GeneratePartialClassContextStub(DebuggerCompletionContext context)
{
var compilation = SD.ParserService.GetCompilationForFile(context.FileName);
var file = SD.ParserService.GetExistingUnresolvedFile(context.FileName);
if (compilation == null || file == null)
return "";
var unresolvedMember = file.GetMember(context.Location);
if (unresolvedMember == null)
return "";
var member = unresolvedMember.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly));
if (member == null)
return "";
var builder = new TypeSystemAstBuilder();
MethodDeclaration decl;
if (unresolvedMember is IMethod) {
// If it's a method, convert it directly (including parameters + type parameters)
decl = (MethodDeclaration)builder.ConvertEntity(member);
} else {
// Otherwise, create a method anyways, and copy the parameters
decl = new MethodDeclaration();
if (member is IParameterizedMember) {
foreach (var p in ((IParameterizedMember)member).Parameters) {
decl.Parameters.Add(builder.ConvertParameter(p));
}
}
}
decl.Name = "__DebuggerStub__";
decl.ReturnType = builder.ConvertType(member.ReturnType);
decl.Modifiers = unresolvedMember.IsStatic ? Modifiers.Static : Modifiers.None;
// Make the method look like an explicit interface implementation so that it doesn't appear in CC
decl.PrivateImplementationType = new SimpleType("__DummyType__");
decl.Body = GenerateBodyFromContext(builder, context);
return WrapInType(unresolvedMember.DeclaringTypeDefinition, decl).ToString();
}
示例4: CreateShortType
public override AstType CreateShortType (IType fullType)
{
AstNode node = Unit.GetNodeAt(Location);
CSharpResolver csResolver = resolver.GetResolverStateBefore(node);
var builder = new TypeSystemAstBuilder (csResolver);
return builder.ConvertType (fullType);
}
示例5: MemberCompletionData
public MemberCompletionData(IType type, IEntity member, TypeSystemAstBuilder builder)
{
DeclarationCategory = DeclarationCategory.Enumeration_Member;
Type = type;
Member = member;
string typeName = builder.ConvertType(type).GetText();
SetDefaultText(typeName + "." + member.Name);
Documentation = member.Documentation;
}
示例6: ImportCompletionData
public ImportCompletionData(ITypeDefinition typeDef, CSharpResolver contextAtCaret, bool useFullName)
: base(typeDef)
{
this.Description = "using " + typeDef.Namespace + ";";
if (useFullName) {
var astBuilder = new TypeSystemAstBuilder(contextAtCaret);
insertionText = astBuilder.ConvertType(typeDef).ToString();
} else {
insertionText = typeDef.Name;
insertUsing = typeDef.Namespace;
}
}
示例7: TypeCompletionData
public TypeCompletionData(ICSharpCode.NRefactory.TypeSystem.IType type, bool fullName, bool isInAttributeContext, TypeSystemAstBuilder builder, bool addForTypeCreation)
{
Type = type;
FullName = fullName;
IsInAttributeContext = isInAttributeContext;
// Confirm that this doesn't also include other types, such as enums, structs, etc.
//DeclarationCategory = DeclarationCategory.Class;
DeclarationCategory = type.Kind.ResolveDeclarationCategoryFromTypeKind();
string typeName = fullName ? builder.ConvertType(type).GetText() : type.Name;
if (isInAttributeContext && typeName.EndsWith("Attribute") && typeName.Length > "Attribute".Length)
{
typeName = typeName.Substring(0, typeName.Length - "Attribute".Length);
}
SetDefaultText(typeName);
// for documentation, see type.GetDefinition().Documentation
Documentation = type.GetDefinition().Documentation;
}
示例8: GenerateBodyFromContext
static void GenerateBodyFromContext(TypeSystemAstBuilder builder, StackFrame context, MethodDeclaration methodDeclaration)
{
methodDeclaration.Body = new BlockStatement();
foreach (var v in context.GetLocalVariables())
methodDeclaration.Body.Statements.Add(new VariableDeclarationStatement(builder.ConvertType(v.Type), v.Name));
methodDeclaration.Body.Statements.Add(new ExpressionStatement(new IdentifierExpression("$__Caret_Point__$")));
}
示例9: GetPossibleNamespaces
static IEnumerable<Tuple<string, bool>> GetPossibleNamespaces (Document doc, AstNode node, ResolveResult resolveResult, DocumentLocation location)
{
var unit = doc.ParsedDocument.GetAst<SyntaxTree> ();
if (unit == null)
yield break;
int tc = GetTypeParameterCount (node);
var attribute = unit.GetNodeAt<ICSharpCode.NRefactory.CSharp.Attribute> (location);
bool isInsideAttributeType = attribute != null && attribute.Type.Contains (location);
var compilation = doc.Compilation;
var lookup = new MemberLookup (null, compilation.MainAssembly);
if (resolveResult is AmbiguousTypeResolveResult) {
var aResult = resolveResult as AmbiguousTypeResolveResult;
var file = doc.ParsedDocument.ParsedFile as CSharpUnresolvedFile;
var scope = file.GetUsingScope (location).Resolve (compilation);
while (scope != null) {
foreach (var u in scope.Usings) {
foreach (var typeDefinition in u.Types) {
if (typeDefinition.Name == aResult.Type.Name &&
typeDefinition.TypeParameterCount == tc &&
lookup.IsAccessible (typeDefinition, false)) {
yield return Tuple.Create (typeDefinition.Namespace, true);
}
}
}
scope = scope.Parent;
}
yield break;
}
if (resolveResult is UnknownIdentifierResolveResult) {
var uiResult = resolveResult as UnknownIdentifierResolveResult;
string possibleAttributeName = isInsideAttributeType ? uiResult.Identifier + "Attribute" : null;
foreach (var typeDefinition in compilation.GetAllTypeDefinitions ()) {
if ((typeDefinition.Name == uiResult.Identifier || typeDefinition.Name == possibleAttributeName) && typeDefinition.TypeParameterCount == tc &&
lookup.IsAccessible (typeDefinition, false)) {
if (typeDefinition.DeclaringTypeDefinition != null) {
var builder = new TypeSystemAstBuilder (new CSharpResolver (doc.Compilation));
yield return Tuple.Create (builder.ConvertType (typeDefinition.DeclaringTypeDefinition).GetText (), false);
} else {
yield return Tuple.Create (typeDefinition.Namespace, true);
}
}
}
yield break;
}
if (resolveResult is UnknownMemberResolveResult) {
var umResult = (UnknownMemberResolveResult)resolveResult;
string possibleAttributeName = isInsideAttributeType ? umResult.MemberName + "Attribute" : null;
foreach (var typeDefinition in compilation.GetAllTypeDefinitions ().Where (t => t.HasExtensionMethods)) {
foreach (var method in typeDefinition.Methods.Where (m => m.IsExtensionMethod && (m.Name == umResult.MemberName || m.Name == possibleAttributeName))) {
IType[] inferredTypes;
if (CSharpResolver.IsEligibleExtensionMethod (
compilation.Import (umResult.TargetType),
method,
true,
out inferredTypes
)) {
yield return Tuple.Create (typeDefinition.Namespace, true);
goto skipType;
}
}
skipType:
;
}
yield break;
}
if (resolveResult is ErrorResolveResult) {
var identifier = unit != null ? unit.GetNodeAt<Identifier> (location) : null;
if (identifier != null) {
var uiResult = resolveResult as UnknownIdentifierResolveResult;
if (uiResult != null) {
string possibleAttributeName = isInsideAttributeType ? uiResult.Identifier + "Attribute" : null;
foreach (var typeDefinition in compilation.GetAllTypeDefinitions ()) {
if ((identifier.Name == uiResult.Identifier || identifier.Name == possibleAttributeName) &&
typeDefinition.TypeParameterCount == tc &&
lookup.IsAccessible (typeDefinition, false))
yield return Tuple.Create (typeDefinition.Namespace, true);
}
}
}
yield break;
}
}
示例10: Main
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Please specify the path to a .sln file on the command line");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
return;
}
Solution solution = new Solution(args[0]);
foreach (var file in solution.AllFiles) {
var astResolver = new CSharpAstResolver(file.Project.Compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile);
foreach (var invocation in file.SyntaxTree.Descendants.OfType<InvocationExpression>()) {
// Retrieve semantics for the invocation
var rr = astResolver.Resolve(invocation) as InvocationResolveResult;
if (rr == null) {
// Not an invocation resolve result - e.g. could be a UnknownMemberResolveResult instead
continue;
}
if (rr.Member.FullName != "System.String.IndexOf") {
// Invocation isn't a string.IndexOf call
continue;
}
if (rr.Member.Parameters.First().Type.FullName != "System.String") {
// Ignore the overload that accepts a char, as that doesn't take a StringComparison.
// (looking for a char always performs the expected ordinal comparison)
continue;
}
if (rr.Member.Parameters.Last().Type.FullName == "System.StringComparison") {
// Already using the overload that specifies a StringComparison
continue;
}
Console.WriteLine(invocation.GetRegion() + ": " + invocation.GetText());
file.IndexOfInvocations.Add(invocation);
}
}
Console.WriteLine("Found {0} places to refactor in {1} files.",
solution.AllFiles.Sum(f => f.IndexOfInvocations.Count),
solution.AllFiles.Count(f => f.IndexOfInvocations.Count > 0));
Console.Write("Apply refactorings? ");
string answer = Console.ReadLine();
if ("yes".Equals(answer, StringComparison.OrdinalIgnoreCase) || "y".Equals(answer, StringComparison.OrdinalIgnoreCase)) {
foreach (var file in solution.AllFiles) {
if (file.IndexOfInvocations.Count == 0)
continue;
// DocumentScript expects the the AST to stay unmodified (so that it fits
// to the document state at the time of the DocumentScript constructor call),
// so we call Freeze() to prevent accidental modifications (e.g. forgetting a Clone() call).
file.SyntaxTree.Freeze();
// AST resolver used to find context for System.StringComparison generation
var compilation = file.Project.Compilation;
var astResolver = new CSharpAstResolver(compilation, file.SyntaxTree, file.UnresolvedTypeSystemForFile);
// Create a document containing the file content:
var document = new StringBuilderDocument(file.OriginalText);
var formattingOptions = FormattingOptionsFactory.CreateAllman();
var options = new TextEditorOptions();
using (var script = new DocumentScript(document, formattingOptions, options)) {
foreach (InvocationExpression expr in file.IndexOfInvocations) {
// Generate a reference to System.StringComparison in this context:
var astBuilder = new TypeSystemAstBuilder(astResolver.GetResolverStateBefore(expr));
IType stringComparison = compilation.FindType(typeof(StringComparison));
AstType stringComparisonAst = astBuilder.ConvertType(stringComparison);
// Alternative 1: clone a portion of the AST and modify it
var copy = (InvocationExpression)expr.Clone();
copy.Arguments.Add(stringComparisonAst.Member("Ordinal"));
script.Replace(expr, copy);
// // Alternative 2: perform direct text insertion
// int offset = script.GetCurrentOffset(expr.RParToken.StartLocation);
// script.InsertText(offset, ", " + stringComparisonAst.GetText() + ".Ordinal");
}
}
File.WriteAllText(Path.ChangeExtension(file.FileName, ".output.cs"), document.Text);
}
}
}
示例11: GenerateBodyFromContext
static BlockStatement GenerateBodyFromContext(TypeSystemAstBuilder builder, DebuggerCompletionContext context)
{
var body = new BlockStatement();
foreach (var v in context.Variables)
body.Statements.Add(new VariableDeclarationStatement(builder.ConvertType(v.Type), v.Name));
body.Statements.Add(new ExpressionStatement(new IdentifierExpression("$__Caret_Point__$")));
return body;
}
示例12: GetActions
IEnumerable<CodeAction> GetActions(BaseRefactoringContext context, Expression targetExpression,
IMember member)
{
var csResolver = context.Resolver.GetResolverStateBefore(targetExpression);
var builder = new TypeSystemAstBuilder(csResolver);
var newType = builder.ConvertType(member.DeclaringType);
string description = string.Format("{0} '{1}'", context.TranslateString("Use base class"), newType.GetText());
yield return new CodeAction(description, script => {
script.Replace(targetExpression, newType);
});
}
开发者ID:adisik,项目名称:simple-assembly-explorer,代码行数:11,代码来源:ReferenceToStaticMemberViaDerivedTypeIssue.cs
示例13: GetShortType
string GetShortType (IType type, CSharpResolver state)
{
var builder = new TypeSystemAstBuilder (state);
var dt = state.CurrentTypeDefinition;
var declaring = type.DeclaringType != null ? type.DeclaringType.GetDefinition () : null;
if (declaring != null) {
while (dt != null) {
if (dt.Equals (declaring)) {
builder.AlwaysUseShortTypeNames = true;
break;
}
dt = dt.DeclaringTypeDefinition;
}
}
var shortType = builder.ConvertType (type);
using (var w = new System.IO.StringWriter ()) {
var visitor = new CSharpOutputVisitor (w, FormattingPolicy);
shortType.AcceptVisitor (visitor, null);
return w.ToString ();
}
}
示例14: GetShortType
string GetShortType(IType type, CSharpResolver state)
{
var builder = new TypeSystemAstBuilder(state);
var dt = state.CurrentTypeDefinition;
var declaring = type.DeclaringType != null ? type.DeclaringType.GetDefinition() : null;
if (declaring != null) {
while (dt != null) {
if (dt.Equals(declaring)) {
builder.AlwaysUseShortTypeNames = true;
break;
}
dt = dt.DeclaringTypeDefinition;
}
}
var shortType = builder.ConvertType(type);
return shortType.GetText(FormattingPolicy);
}
示例15: GetActions
IEnumerable<CodeAction> GetActions(ParameterDeclaration parameter, IEnumerable<IType> possibleTypes)
{
var csResolver = ctx.Resolver.GetResolverStateBefore(parameter);
var astBuilder = new TypeSystemAstBuilder(csResolver);
foreach (var type in possibleTypes) {
var localType = type;
var message = string.Format(ctx.TranslateString("Demote parameter to '{0}'"), type.FullName);
yield return new CodeAction(message, script => {
script.Replace(parameter.Type, astBuilder.ConvertType(localType));
});
}
}