Roslyn是否有可能弄清楚一个类方法是否实现了一些用属性标记的接口方法?
特别是在wcf中,我们使用接口来描述服务合同.它的每种方法都应该OperationContractAttribute在下面的样本中标记
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
在Roslyn ISymbol接口中提供了GetAttributes()方法,但是在SimleService.GetData()它返回0 的方法上调用.在ISimleService.GetData()声明上调用它OperationContractAttribute按预期返回.
所以一般来说我必须检查类层次结构以找到所有实现的接口,然后遍历层次结构以找到合适的方法.这是一个很难的方法,我想应该有一个更简单的方法.
是的,可以找出方法是否是接口方法的实现.这是执行此操作的代码:
methodSymbol.ContainingType
.AllInterfaces
.SelectMany(@interface => @interface.GetMembers().OfType<IMethodSymbol>())
.Any(method => methodSymbol.Equals(methodSymbol.ContainingType.FindImplementationForInterfaceMember(method)));
Run Code Online (Sandbox Code Playgroud)
您可以修改它以实际获取已实现的方法,然后您可以检查属性.
| 归档时间: |
|
| 查看次数: |
979 次 |
| 最近记录: |