int num = 10; string str = ""; SyntaxTree tree = CSharpSyntaxTree.ParseText("string s = num"); SemanticModel model = compilation.GetSemanticModel(tree); Conversion classification = model.ClassifyConversion(num, model.GetTypeInfo(tree.GetRoot().DescendantNodes().OfType().First().Type)); // We can then check if the conversion is implicit or explicit if (classification.IsImplicit) { str = num.ToString(); } else { str = Convert.ToString(num); }
string s = "5"; int num = 0; SyntaxTree tree = CSharpSyntaxTree.ParseText("int i = s"); SemanticModel model = compilation.GetSemanticModel(tree); Conversion classification = model.ClassifyConversion(model.GetTypeInfo(tree.GetRoot().DescendantNodes().OfTypeThis example determines if the conversion from string to int is implicit or explicit and then executes a different method based on the result. The package library used in these examples is the Roslyn library, which is part of the Microsoft.CodeAnalysis namespace.().First().Initializer.Value), model.GetTypeInfo(tree.GetRoot().DescendantNodes().OfType ().First().Type)); // We can then check if the conversion is implicit or explicit if (classification.IsImplicit) { num = int.Parse(s); } else { num = Convert.ToInt32(s); }