本文整理汇总了C#中SemanticModel.GetConversion方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetConversion方法的具体用法?C# SemanticModel.GetConversion怎么用?C# SemanticModel.GetConversion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.GetConversion方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOuterCastType
private static ITypeSymbol GetOuterCastType(ExpressionSyntax expression, SemanticModel semanticModel, out bool parentIsOrAsExpression)
{
expression = expression.WalkUpParentheses();
parentIsOrAsExpression = false;
var parentNode = expression.Parent;
if (parentNode == null)
{
return null;
}
if (parentNode.IsKind(SyntaxKind.CastExpression))
{
var castExpression = (CastExpressionSyntax)parentNode;
return semanticModel.GetTypeInfo(castExpression).Type;
}
if (parentNode.IsKind(SyntaxKind.IsExpression) ||
parentNode.IsKind(SyntaxKind.AsExpression))
{
parentIsOrAsExpression = true;
return null;
}
if (parentNode.IsKind(SyntaxKind.ArrayRankSpecifier))
{
return semanticModel.Compilation.GetSpecialType(SpecialType.System_Int32);
}
if (parentNode.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
var memberAccess = (MemberAccessExpressionSyntax)parentNode;
if (memberAccess.Expression == expression)
{
var memberSymbol = semanticModel.GetSymbolInfo(memberAccess).Symbol;
if (memberSymbol != null)
{
return memberSymbol.ContainingType;
}
}
}
if (parentNode.IsKind(SyntaxKind.ConditionalExpression) &&
((ConditionalExpressionSyntax)parentNode).Condition == expression)
{
return semanticModel.Compilation.GetSpecialType(SpecialType.System_Boolean);
}
if ((parentNode is PrefixUnaryExpressionSyntax || parentNode is PostfixUnaryExpressionSyntax) &&
!semanticModel.GetConversion(expression).IsUserDefined)
{
var parentEpression = (ExpressionSyntax)parentNode;
return GetOuterCastType(parentEpression, semanticModel, out parentIsOrAsExpression) ?? semanticModel.GetTypeInfo(parentEpression).ConvertedType;
}
return null;
}
示例2: ConversionTestHelper
/// <summary>
///
/// </summary>
/// <param name="binding"></param>
/// <param name="expr"></param>
/// <param name="ept1">expr -> TypeInParent</param>
/// <param name="ept2">Type(expr) -> TypeInParent</param>
private void ConversionTestHelper(SemanticModel semanticModel, ExpressionSyntax expr, ConversionKind ept1, ConversionKind ept2)
{
var info = semanticModel.GetTypeInfo(expr);
Assert.NotNull(info);
Assert.NotNull(info.ConvertedType);
var conv = semanticModel.GetConversion(expr);
// NOT expect NoConversion
Conversion act1 = semanticModel.ClassifyConversion(expr, (TypeSymbol)info.ConvertedType);
Assert.Equal(ept1, act1.Kind);
ValidateConversion(act1, ept1);
ValidateConversion(act1, conv.Kind);
if (ept2 == ConversionKind.NoConversion)
{
Assert.Null(info.Type);
}
else
{
Assert.NotNull(info.Type);
var act2 = semanticModel.Compilation.ClassifyConversion(info.Type, info.ConvertedType);
Assert.Equal(ept2, act2.Kind);
ValidateConversion(act2, ept2);
}
}
示例3: GetAnonymousExprSymbols
private void GetAnonymousExprSymbols(ExpressionSyntax expr, SemanticModel model, List<ISymbol> list)
{
var kind = expr.Kind();
if (kind != SyntaxKind.AnonymousObjectCreationExpression &&
kind != SyntaxKind.AnonymousMethodExpression &&
kind != SyntaxKind.ParenthesizedLambdaExpression &&
kind != SyntaxKind.SimpleLambdaExpression)
{
return;
}
var tinfo = model.GetTypeInfo(expr);
var conv = model.GetConversion(expr);
if (conv.IsAnonymousFunction)
{
// Lambda has no Type unless in part of case expr (C# specific)
// var f = (Func<int>)(() => { return 1; }); Type is delegate
// method symbol
var sinfo = model.GetSymbolInfo(expr);
list.Add((Symbol)sinfo.Symbol);
}
else if (tinfo.Type != null && tinfo.Type.TypeKind != TypeKind.Delegate)
{
// bug#12625
// GetSymbolInfo -> .ctor (part of members)
list.Add((Symbol)tinfo.Type); // NamedType with empty name
foreach (var m in tinfo.Type.GetMembers())
{
list.Add((Symbol)m);
}
}
}
示例4: GetRegularExpressionType
private static ITypeSymbol GetRegularExpressionType(SemanticModel semanticModel, SyntaxNode node)
{
// regular case. always use ConvertedType to get implicit conversion right.
var expression = node.GetUnparenthesizedExpression();
var info = semanticModel.GetTypeInfo(expression);
var conv = semanticModel.GetConversion(expression);
if (info.ConvertedType == null || info.ConvertedType.IsErrorType())
{
// there is no implicit conversion involved. no need to go further
return info.Type;
}
// always use converted type if method group
if ((!node.IsKind(SyntaxKind.ObjectCreationExpression) && semanticModel.GetMemberGroup(expression).Length > 0) ||
IsCoClassImplicitConversion(info, conv, semanticModel.Compilation.CoClassType()))
{
return info.ConvertedType;
}
// check implicit conversion
if (conv.IsImplicit && (conv.IsConstantExpression || conv.IsEnumeration))
{
return info.ConvertedType;
}
// always try to use type that is more specific than object type if possible.
return !info.Type.IsObjectType() ? info.Type : info.ConvertedType;
}