Roslyn get IdentifierName in ObjectCreationExpressionSyntax - c#

Currently I am working on simple code analyse for c# with roslyn. I need to parse all document of all projects inside one solution and getting the declared used classes inside this document.
For example from:
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
}
}
I want to get Program uses Foo.
I already parse all documents and get the declared class inside.
// all projects in solution
foreach (var project in _solution.Projects)
{
// all documents inside project
foreach (var document in project.Documents)
{
var syntaxRoot = await document.GetSyntaxRootAsync();
var model = await document.GetSemanticModelAsync();
var classes = syntaxRoot.DescendantNodes().OfType<ClassDeclarationSyntax>();
// all classes inside document
foreach (var classDeclarationSyntax in classes)
{
var symbol = model.GetDeclaredSymbol(classDeclarationSyntax);
var objectCreationExpressionSyntaxs = classDeclarationSyntax.DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
// all object creations inside document
foreach (var objectCreationExpressionSyntax in objectCreationExpressionSyntaxs)
{
// TODO: Get the identifier value
}
}
}
}
The problem is to get the IdentifierName Foo. Using the debugger, I see objectCreationExpressionSyntax.Typegot the Identifier.Text got the value I need, but objectCreationExpressionSyntax.Type.Identifierseems to be private.
I could use the SymbolFinder to find all references of a Class in the solution. As I already need to parse all documents its should work without.
Maybe I am on the wrong path? How to get the identifier value?

You'll need to handle the different types of TypeSyntaxes. See here: http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Syntax/TypeSyntax.cs,29171ac4ad60a546,references
What you see in the debugger is a SimpleNameSyntax, which does have a public Identifier property.
Update
var ns = objectCreationExpressionSyntax.Type as NameSyntax;
if (ns != null)
{
return ns.Identifier.ToString();
}
var pts = objectCreationExpressionSyntax.Type as PredefinedTypeSyntax;
if (pts != null)
{
return pts.Keyword.ToString();
}
...
All other subtypes would need to be handed. Note that ArrayType.ElementType is also a TypeSyntax, so you would most probably need to make this method recursive.

You can get the identifier from the syntax's Type property:
foreach (var objectCreationExpressionSyntax in objectCreationExpressionSyntaxs)
{
IdentifierNameSyntax ins = (IdentifierNameSyntax)objectCreationExpressionSyntax.Type;
var id = ins.Identifier;
Console.WriteLine(id.ValueText);
}

Strings can be misleading.
Let's say you have the expression new SomeClass(), and you get the string "SomeClass" out of it. How do you know if that refers to Namespace1.SomeClass or Namespace2.SomeClass ? What if there is a using SomeClass = Namespace3.SomeOtherType; declaration being used?
Fortunately, you don't have to do this analysis yourself. The compiler can bind the ObjectCreationExpressionSyntax to a symbol. You have your semantic model, use it.
foreach (var oce in objectCreationExpressionSyntaxs)
{
ITypeSymbol typeSymbol = model.GetTypeInfo(oce).Type;
// ...
}
You can compare this symbol with the symbols you get from model.GetDeclaredSymbol(classDeclarationSyntax), just make sure you use the Equals method, not the == operator.

Related

Compiling methods for optimal runtime performance

I'm trying to determine what the best way to get fully optimized delegates for various objects to improve the speed of serialization. Simply put: I'd like to remove various different checks, and compile more efficient serialize functions one time at the start of my app.
Let's take a look at this simple example:
public class GamePacket
{
[Length(10)]
[ReadBackwards]
public string Id { get; set; }
}
Now, I'd likely create a serializer, and for performance reasons store the attributes in a cached field. Everytime I want to deserialize a GamePacket from a stream (or byte array), I'd call something like:
Deserialize(byte[] stream)
{
var header = stream.ReadByte();
var packet = cachedDeserializers[header];
var instance = packet.DelegateForCreateInstance();
foreach (var field in packet.Fields)
{
if (field.Type != TypeCode.String) continue;
var str = stream.ReadBytes(field.LengthAttribute.Length);
if (field.HasReadBackwardsAttribute)
str = str.Reverse();
field.DelegateForSetValue(instance, str);
}
}
The problem now lies in the fact that EVERY time I'm calling Deserialize on that stream, I need to loop through and check various things like attributes, and other checks. In the example, these things can potentially be omitted (And maybe more):
if (field.Type != TypeCode.String) continue;
if (field.HasReadBackwardsAttribute)
If I know the field has a read backwards attribute, I'd like to compile a simplified delegate on app start that omits these checks, and simply reads it backwards. Is it possible to create a delegate that can remove unneeded logic? For example:
Deserialize(byte[] stream)
{
var header = stream.ReadByte();
var packet = cachedDeserializers[header];
var instance = packet.CallCachedCompile(stream);
}
// CallCachedCompile for GamePacket would look something like this:
CallCachedCompile(byte[] stream)
{
var instance = this.DelegateForCreateInstance();
var str = stream.ReadBytes(10);
str = str.Reverse();
this.DelegateForSetValue(instance, "Id", str);
return instance;
}
I've looked briefly into expression trees. Would something like this be doable in expression Trees? What would be the most efficient way?
Yes, using code generation approach you can generate delegates for the particular type. So instead of this generic reflection-like code:
foreach (var field in packet.Fields)
{
if (field.Type != TypeCode.String) continue;
var str = stream.ReadBytes(field.LengthAttribute.Length);
if (field.HasReadBackwardsAttribute)
str = str.Reverse();
field.DelegateForSetValue(instance, str);
}
You can generate code for a specific type:
gamePacketInstance.Id = SomeConvertionToString(stream.ReadBytes(field.LengthAttribute.Length).Revers());
The code generation topic is quite big and I don't know what exactly you do inside your delegates. You can generate specific delegates in runtime (emit il or expression trees) or in compile time (source generators). I suggest you to read my article Dotnet code generation overview by example. It will give good overview with examples.
Without using any code generation you can still do this pretty efficiently.
You basically need to use generics and polymorphism to cache all code that you want done for each type that you encounter.
Bearing in mind that properties have underlying methods, we can create delegates to set properties without using code generation.
abstract class DeserializerBase
{
object DeserializePacket(Stream stream);
}
class Deserializer<T> : DeserializerBase where T : new()
{
FieldAction<T>[] fieldActions =
typeof(T).GetProperties()
.Where(p => p.Type == TypeCode.String)
.Select(p => IsReverseAttribute(p)
? new FieldActionReverse<T>
{
Setter = p.SetMethod.CreateDelegate<Action<T, string>>(),
Length = GetLengthAttribute(p),
}
: new FieldAction<T>
{
Setter = p.SetMethod.CreateDelegate<Action<T, string>>(),
Length = GetLengthAttribute(p),
})
.ToArray();
object DeserializePacket(Stream stream);
{
var packet = new T();
foreach (var action in fieldActions)
action.Deserialize(packet);
}
}
class FieldAction<T>
{
public Action<T, string> Setter;
public int Length;
void Deserialize(Stream stream, T instance)
{
var str = ReadString(stream);
Setter(instance, str);
}
virtual string GetString(Stream stream)
{
return stream.ReadBytes(Length);
}
}
class FieldActionReverse<T> : FieldAction<T>
{
override string GetString(Stream stream)
{
return stream.ReadBytes(Length).Reverse();
}
}
Your final entry code becomes this.
Dictionary<int, DeserializerBase> cachedDeserializers = new Dictionary<int, DeserializerBase>
{
{5, new Deserializer<GamePacket>()}
};
Deserialize(Stream stream)
{
var header = stream.ReadByte();
var packet = cachedDeserializers[header].DeserializePacket(stream);
}
You can even place generic constraints on T to ensure it is a Packet then you can return a base Packet type from this entry function.

Merging Two .CS Files in C# to generate a new Class

I want to merge two .cs files to create a third one. Can anyone help me please.
public partial class A
{
// some methods
}
suppose this code is written in a file A.cs
public partial class B
{
// some methods
}
and this code is written in a file B.cs.
I want to generate a new C.cs
having all the code of A.cs and B.cs ignoring namespaces.
I assume you indeed want to merge the partial definitions of the same class. If you really need to merge different classes into a single one, the code can be easily adjusted, but there will be no guarantee that it compiles (because, for example, the classes could have members with the same name).
The problem is quite complicated indeed because of the symbol meaning: it depends on the usings, so one needs to be really careful when merging them.
So the best idea would be not to try to analyse the code semantics manually, but to use a big hammer: Roslyn analyzer.
Let's start.
First of all, you'll need to install Extension Development Workload as it's described here. After this, you'll be able to create a Standalone code analysis tool project.
When you create it, you'll get a lot of useful boilerplate code like this:
class Program
{
static async Task Main(string[] args)
{
// ...
using (var workspace = MSBuildWorkspace.Create())
{
var solutionPath = args[0];
WriteLine($"Loading solution '{solutionPath}'");
var solution = await workspace.OpenSolutionAsync(solutionPath,
new ConsoleProgressReporter());
WriteLine($"Finished loading solution '{solutionPath}'");
// insert your code here
}
}
private static VisualStudioInstance SelectVisualStudioInstance(
VisualStudioInstance[] visualStudioInstances)
{
// ...
}
private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
{
// ...
}
}
Let's fill in what is needed.
Instead of // insert your code here let's put the following code:
var targetClass = args[1];
var modifiedSolution = await MergePartialClasses(targetClass, solution);
workspace.TryApplyChanges(modifiedSolution);
We'll need to implement the logic in MergePartialClasses. The name of the class should be passed as the second command line parameter.
Let's first add the following usings at the top:
using static System.Console;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Now we can start with the main method. I've put the comments about what's happening directly in the code.
static async Task<Solution> MergePartialClasses(string targetClass, Solution solution)
{
// https://stackoverflow.com/a/32179708/276994
// we loop through the projects in the solution and process each of the projects
foreach (var projectId in solution.ProjectIds)
{
var project = solution.GetProject(projectId);
WriteLine($"Processing project {project.Name}");
var compilation = await project.GetCompilationAsync();
// finding the type which we want to merge
var type = compilation.GetTypeByMetadataName(targetClass);
if (type == null)
{
WriteLine($"Type {targetClass} is not found");
return solution;
}
// look up number of declarations. if it's only 1, we have nothing to merge
var declarationRefs = type.DeclaringSyntaxReferences;
if (declarationRefs.Length <= 1)
{
WriteLine($"Type {targetClass} has only one location");
return solution;
}
// I didn't implement the case of nested types, which would require to
// split the outer class, too
if (type.ContainingType != null)
throw new NotImplementedException("Splitting nested types");
// we'll accumulate usings and class members as we traverse all the definitions
var accumulatedUsings = new List<UsingDirectiveSyntax>();
var classParts = new List<ClassDeclarationSyntax>();
foreach (var declarationRef in declarationRefs)
{
var declaration = (ClassDeclarationSyntax)await declarationRef.GetSyntaxAsync();
// get hold of the usings
var tree = declaration.SyntaxTree;
var root = await tree.GetRootAsync();
var usings = root.DescendantNodes().OfType<UsingDirectiveSyntax>();
accumulatedUsings.AddRange(usings);
// since we are trying to move the syntax into another file,
// we need to expand everything in order to remove the dependency
// on usings
// in order to do it, we use a custom CSharpSyntaxRewriter (defined later)
var document = project.GetDocument(tree);
var expander = new AllSymbolsExpander(document);
var expandedDeclaration = (ClassDeclarationSyntax)expander.Visit(declaration);
classParts.Add(expandedDeclaration);
// remove the old declaration from the place where it is
// we can't just remove the whole file as it may contain some other classes
var modifiedRoot =
root.RemoveNodes(new[] { declaration }, SyntaxRemoveOptions.KeepNoTrivia);
var modifiedDocument = document.WithSyntaxRoot(modifiedRoot);
project = modifiedDocument.Project;
}
// now, sort the usings and remove the duplicates
// in order to use DistinctBy, I added MoreLinq nuget package and added
// using MoreLinq; at the beginning
// https://stackoverflow.com/a/34063289/276994
var sortedUsings = accumulatedUsings
.DistinctBy(x => x.Name.ToString())
.OrderBy(x => x.StaticKeyword.IsKind(SyntaxKind.StaticKeyword) ?
1 : x.Alias == null ? 0 : 2)
.ThenBy(x => x.Alias?.ToString())
.ThenByDescending(x => x.Name.ToString().StartsWith(nameof(System) + "."))
.ThenBy(x => x.Name.ToString());
// now, we have to merge the class definitions.
// split the name into namespace and class name
var (nsName, className) = SplitName(targetClass);
// gather all the attributes
var attributeLists = List(classParts.SelectMany(p => p.AttributeLists));
// modifiers must be the same, so we are taking them from the
// first definition, but remove partial if it's there
var modifiers = classParts[0].Modifiers;
var partialModifier = modifiers.FirstOrDefault(
m => m.Kind() == SyntaxKind.PartialKeyword);
if (partialModifier != null)
modifiers = modifiers.Remove(partialModifier);
// gather all the base types
var baseTypes =
classParts
.SelectMany(p => p.BaseList?.Types ?? Enumerable.Empty<BaseTypeSyntax>())
.Distinct()
.ToList();
var baseList = baseTypes.Count > 0 ? BaseList(SeparatedList(baseTypes)) : null;
// and constraints (I hope that Distinct() works as expected)
var constraintClauses =
List(classParts.SelectMany(p => p.ConstraintClauses).Distinct());
// now, we construct class members by pasting together the accumulated
// per-part member lists
var members = List(classParts.SelectMany(p => p.Members));
// now we can build the class declaration
var classDef = ClassDeclaration(
attributeLists: attributeLists,
modifiers: modifiers,
identifier: Identifier(className),
typeParameterList: classParts[0].TypeParameterList,
baseList: baseList,
constraintClauses: constraintClauses,
members: members);
// if there was a namespace, let's put the class inside it
var body = (nsName == null) ?
(MemberDeclarationSyntax)classDef :
NamespaceDeclaration(IdentifierName(nsName)).AddMembers(classDef);
// now create the compilation unit and insert it into the project
// http://roslynquoter.azurewebsites.net/
var newTree = CompilationUnit()
.WithUsings(List(sortedUsings))
.AddMembers(body)
.NormalizeWhitespace();
var newDocument = project.AddDocument(className, newTree);
var simplifiedNewDocument = await Simplifier.ReduceAsync(newDocument);
project = simplifiedNewDocument.Project;
solution = project.Solution;
}
// finally, return the modified solution
return solution;
}
The rest is the AllSymbolsExpander, which just calls Simplifier.ExpandAsync for every node:
class AllSymbolsExpander : CSharpSyntaxRewriter
{
Document document;
public AllSymbolsExpander(Document document)
{
this.document = document;
}
public override SyntaxNode VisitAttribute(AttributeSyntax node) =>
Expand(node);
public override SyntaxNode VisitAttributeArgument(AttributeArgumentSyntax node) =>
Expand(node);
public override SyntaxNode VisitConstructorInitializer(ConstructorInitializerSyntax node) =>
Expand(node);
public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node) =>
Expand(node);
public override SyntaxNode VisitXmlNameAttribute(XmlNameAttributeSyntax node) =>
Expand(node);
public override SyntaxNode VisitTypeConstraint(TypeConstraintSyntax node) =>
Expand(node);
public override SyntaxNode DefaultVisit(SyntaxNode node)
{
if (node is ExpressionSyntax ||
node is StatementSyntax ||
node is CrefSyntax ||
node is BaseTypeSyntax)
return Expand(node);
return base.DefaultVisit(node);
}
SyntaxNode Expand(SyntaxNode node) =>
Simplifier.ExpandAsync(node, document).Result; //? async-counterpart?
}
and the trivial function SplitName:
static (string, string) SplitName(string name)
{
var pos = name.LastIndexOf('.');
if (pos == -1)
return (null, name);
else
return (name.Substring(0, pos), name.Substring(pos + 1));
}
That's all!
I wanted to merge all code generated files to create one file. After a lot of searches I achieved this task by creating a new class. Read all code first generated files, create their objects in my newly created class and call their Up() and Down() methods in it.
Note: Wrote a separate method a read all namespaces distinct. If anyone wants code I can share my code sample too.

Modifying function declaration parameter by adding attribute and default value in Roslyn c#

I'm modifying my earlier code analyser in C# using Roslyn and again I'm stuck with some changes that I don't exactly know how to apply.
Basing on: https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis I've created some base to work with in this question: Finding all not inheriting C# classes with Roslyn and changing to inheriting from base object (java-like)
I've parsed and traversed tree to find all method declarations, and their parameters. Basing on VS Syntax Visualiser I've constructed this:
foreach (var c in root1.DescendantNodesAndSelf())
{
var methodDeclaration = c as MethodDeclarationSyntax;
if (methodDeclaration == null)
continue;
if (methodDeclaration.ParameterList != null) //Have parameters
{
foreach (var p in methodDeclaration.ParameterList.Parameters)
{
var parameter = p as ParameterSyntax;
String name, type;
name = parameter.GetLastToken().Value.ToString();
type = parameter.GetFirstToken().Value.ToString();
if (parameter == null)
continue;
if (name == "caller" && type == "string")
{
AttributeSyntax ats = SyntaxFactory.Attribute(SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System.Runtime.CompilerServices"),SyntaxFactory.IdentifierName("CallerMemberName")));
SeparatedSyntaxList<AttributeSyntax> ssl = new SeparatedSyntaxList<AttributeSyntax>();
ssl = ssl.Add(ats);
AttributeListSyntax als = SyntaxFactory.AttributeList(ssl);
var par1 = parameter.AddAttributeLists(als);
//ExpressionSyntax es = SyntaxFactory.AssignmentExpression(SyntaxKind.EqualsValueClause,null,SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression))
//SyntaxFactory.EqualsValueClause(es);
par1 = par1.AddModifiers();
root2 = root2.ReplaceNode(parameter, par1);
}
}
}
else //Don't have parameters
continue;
}
I'm trying to convert method declared like this:
private void testM3(string caller)
into
private void testM3([System.Runtime.CompilerServices.CallerMemberName] string caller = "")
And this part:
//ExpressionSyntax es = SyntaxFactory.AssignmentExpression(SyntaxKind.EqualsValueClause,null,SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression))
//SyntaxFactory.EqualsValueClause(es);
is my failed attempt to achieve creation of equals node.
From what I understand, this part:
AttributeSyntax ats = SyntaxFactory.Attribute(SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System.Runtime.CompilerServices"),SyntaxFactory.IdentifierName("CallerMemberName")));
SeparatedSyntaxList<AttributeSyntax> ssl = new SeparatedSyntaxList<AttributeSyntax>();
ssl = ssl.Add(ats);
AttributeListSyntax als = SyntaxFactory.AttributeList(ssl);
var par1 = parameter.AddAttributeLists(als);
will give me new parameter node in var par1 that includes attribute already, so I need to add default value setting. Please correct me if I'm wrong about this attribute, and I'd like to know how to build this equals expression node properly.
You have a two mistakes:
System.Runtime.CompilerServices.CallerMemberName is QualifiedName that contains System.Runtime.CompilerServices as QualifiedName and CallerMemberName as IdentifierName. System.Runtime.CompilerServices contains System.Runtime as QualifiedName and CompilerServices as IdentifierName. And finally System.Runtime contains two IdentifierName.
So you need to fix creation of AttributeSyntax as are shown in the code below:
AttributeSyntax ats = SyntaxFactory.Attribute(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.IdentifierName("Runtime")),
SyntaxFactory.IdentifierName("CompilerServices")),
SyntaxFactory.IdentifierName("CallerMemberName")));
EqualsValueClause shouldn't contain AssignmentExpression, but should contains a some kind of LiteralExpression directly. In your case it's StringLiteralExpression:
var par1 = parameter
.AddAttributeLists(als)
.WithDefault(SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(""))));
By the way, you can use a some helpful method of Nodes, as example ParameterSyntax.WithDefault, to create a copy of Node (SyntaxTree is immutable in Roslyn) when you want to applay a small changes to existing node and then repace it.
If you take a look at the Roslyn Quoter for the following method you can obtain the code required to generate the desired code:
public void GetSomething([CallerMemberName] string test=""){
}
You will notice that the Default Value in the Parameter is constructed using the following method (the roslyn quoter usually omits the SyntaxFactory):
.WithDefault(SyntaxFactory.EqualsValueClause(
SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal("")
)
)
);
So in order to add your EqualsValueClause as a default you just have to replace the existing default by calling the above code on your parameter (instead of the uncommented code):
par1 = par1.WithDefault(SyntaxFactory.EqualsValueClause(
SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal("")
)
)
);

Get and save enum using reflection

I have a project in which I have some assemblies which implement an abstract class.
Each assembly has a public enum called ResultEnum.
This ResultEnum's value is stored in a database as an int.
I have another web project which displays some info, and I want it to also display this int's string representation - the name of the corresponding value from the ResultEnum.
What I want to do is, using MEF, load all the relevant assemblies (no problem here), search for this enum using reflection (no problem here also) and then to store the enum in some way, and cache it in order to avoid all this process the next time I want to convert the int from the database to the string representation (and the other way around if necessary) since I have several thousands of records in my db table.
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(path));
_container = new CompositionContainer(catalog);
try
{
_container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
foreach (var task in myTasks)
{
TaskAbstract instance = (TaskAbstract)task.CreateExport().Value;
MemberInfo[] infos = instance.GetType().GetMembers(BindingFlags.Public | BindingFlags.Static);
foreach (MemberInfo member in infos.Where(x => x.Name.Equals("ResultEnum")))
{
Console.WriteLine(member);
}
}
What do you suggest the next move should be?
How should I store/cache it?
Thanks
In addition to #Thomas's answer:
As using reflection you can get exact int value from a property, the name for that concrete value could be gotten using the next expression:
var enumValueName = Enum.GetName(member.GetType(), member.GetValue(instance));
UPD
I really missed that you reflect MemberInfos. To apply my solution you can update you reflection this way:
foreach (var task in myTasks)
{
TaskAbstract instance = (TaskAbstract)task.CreateExport().Value;
// Reflect properties, not all members
PropertyInfo[] infos = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static);
foreach (PropertyInfo prop in infos.Where(x => x.Name.Equals("ResultEnum")))
{
var enumValueName = Enum.GetName(prop.GetType(), prop.GetValue(instance));
}
}
Or you could cast MemberInfo to PropertyInfo.
One approach to solving this problem is to consider using a subclassable enums technique (also sometimes referred to as a polymorphic enum).
I wrote a couple of generic classes specifically to support these kinds of types which you can read about here. Also, a proposal has been submitted to the Roslyn compiler team on Github to add support for these types of enums to C#.
Here is an example of a set of subclassable enums that have two underlying types, string and integer, using the classes from my project:
public sealed class Status : StringIntegerEnum<Status>
{
public static readonly Status Active = new Status("active", 1);
public static readonly Status Inactive = new Status("inactive", 0);
private Status(string status, int statusCode) : base(status, statusCode) {}
}
Note that the string value is not the same as the constant name itself, which allows you to have underlying string values with characters that violate the normal naming conventions in C#.
The StringIntegerEnum<tStringIntegerEnum> base class provides .AllValues, .AllNaturalValues and .AllStringValues static methods that you can use to enumerate the list of enum values or both types of their underlying values.
From your comment:
I agree, But question is how do I iterate over the values and names of the enumerator
I assume you mean "enumeration", not "enumerator". You can use the Enum.GetValues method:
var valuesToNames =
Enum.GetValues(enumType)
.Cast<object>()
.ToDictionary(o => (int)o, o => Enum.GetName(enumType, o));
And, is there a better solution than a dictionary
Better how? I think a dictionary is a fine solution; is there any reason why you would want something else?
This is how i've written the code eventually:
resultEnumsForTasks = new Dictionary<string, Dictionary<UInt16, string>>();
foreach (var task in myTasks)
{
Dictionary<UInt16, string> _enum = new Dictionary<UInt16,string>();
TaskAbstract instance = (TaskAbstract)task.CreateExport().Value;
MemberInfo resultEnum = instance.GetType().GetMember("ResultEnum").FirstOrDefault();
if (resultEnum == null)
continue;
string[] names = Enum.GetNames(resultEnum as Type);
IList<int> vals = (IList<int>)Enum.GetValues(resultEnum as Type);
for (int i = 0; i < names.Length; i++)
{
_enum.Add(Convert.ToUInt16(vals[i]), names[i]);
}
resultEnumsForTasks.Add(instance.GetType().Name, _enum);
}
It's very similar to #n.turakulov 's solution, however his solution didn't work for me since I got an empty list of PropertyInfo for some reason...
Thanks for everyone who assisted!

How to get a Roslyn FieldSymbol from a FieldDeclarationSyntax node?

I'm trying to use Roslyn to determine the publically-exposed API of a project (and then do some further processing using this information, so I can't just use reflection). I'm using a SyntaxWalker to visit declaration syntax nodes, and calling IModel.GetDeclaredSymbol for each. This seems to work well for Methods, Properties, and Types, but it doesn't seem to work on fields. My question is, how do I get the FieldSymbol for a FieldDeclarationSyntax node?
Here's the code I'm working with:
public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
{
var model = this._compilation.GetSemanticModel(node.SyntaxTree);
var symbol = model.GetDeclaredSymbol(node);
if (symbol != null
&& symbol.CanBeReferencedByName
// this is my own helper: it just traverses the publ
&& symbol.IsExternallyPublic())
{
this._gatherer.RegisterPublicDeclaration(node, symbol);
}
base.VisitFieldDeclaration(node);
}
You need to remember that a field declaration syntax can declare multiple fields. So you want:
foreach (var variable in node.Declaration.Variables)
{
var fieldSymbol = model.GetDeclaredSymbol(variable);
// Do stuff with the symbol here
}

Categories

Resources