I would like to perform reflection style operations on the following class using Roslyn:
public abstract class MyBaseClass
{
public bool Method1()
{
return true;
}
public bool Method2()
{
return true;
}
public void Method3()
{
}
}
Basically I want to do this, but with Roslyn:
BindingFlags flags = BindingFlags.Public |
BindingFlags.Instance;
MethodInfo[] mBaseClassMethods = typeof(MyBaseClass).GetMethods(flags);
foreach (MethodInfo mi in mBaseClassMethods)
{
if (mi.GetParameters().Length == 0 && mi.ReturnType == typeof(void))
{
methodInfos.Add(mi);
}
if (mi.GetParameters().Length == 0 && mi.ReturnType == typeof(bool))
{
methodInfos.Add(mi);
}
}
Essentially, I would like to get a list of the methods that meet the criteria I used in the reflection example above. Also, if anyone knows of a site that explains how to do Reflection like operations with Roslyn please feel free to point me in that direction. I’ve been searching for hours and can’t seem to make progress on this.
Thanks in advance,
Bob
Getting the methods you want can be done like this:
You’ll need a SyntaxTree for that. With reflection you’re working with assemblies, so I don’t know the answer to that part of your question. If you want this as a Roslyn extension for Visual Studio, then this should be what you’re looking for.