Is it possible with Roslyn to figure out if a class method implements some interface method which was marked with attribute?
Particularly in wcf we describe service contracts using interface. Each method of it should be marked with OperationContractAttribute
like in following sample
[ServiceContract]
public interface ISimleService
{
[OperationContract]
string GetData(int value);
}
public class SimleService : ISimleService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
In Roslyn ISymbol
interface provides us GetAttributes() method, but called on a SimleService.GetData()
method it returns 0. Called on ISimleService.GetData()
declaration it returns OperationContractAttribute
as expected.
So in general I have to check class hierarchy to find all interfaces implemented and then traverse hierarchy to find appropriate methods.This is a hard way and I guess there should be an easier one.
Yes it's possible to find out if a method is an implementation of an interface's method. Here's the code to do that:
methodSymbol.ContainingType
.AllInterfaces
.SelectMany(@interface => @interface.GetMembers().OfType<IMethodSymbol>())
.Any(method => methodSymbol.Equals(methodSymbol.ContainingType.FindImplementationForInterfaceMember(method)));
You can modify this to actually grab the implemented method, and on that you can check the attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With