如何检查类是否在NRefactory中实现接口方法

时间:2013-12-12 15:14:08

标签: c# nrefactory

我有两个档案。其中一个是类声明,另一个是接口声明。类应该实现接口。如果类实现接口方法,我怎样才能检入NRefactory?

我应该提供更多细节。

第一个文件 - 例如:

class Test : IF
{
}

和第二个

interface IF
{
    void Foo();
}

我必须阅读这些文件并使用NRefactory进行解析。我需要检查类Test是否从接口IF实现方法。

没有编译和加载编译的程序集。

3 个答案:

答案 0 :(得分:3)

使用is关键字

http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

if (myObj is IMyInterface) {
 ....
}

答案 1 :(得分:3)

我在NRefactory代码中找到了解决方案。我修改了这个以实现我的目标。首先,我们应该实现访问者,检查类是否从接口实现每个方法:

public class MissingInterfaceMemberImplementationVisitor : DepthFirstAstVisitor
{
    private readonly CSharpAstResolver _resolver;
    public bool IsInterfaceMemberMissing { get; private set; }
    public MissingInterfaceMemberImplementationVisitor(CSharpAstResolver resolver)
    {
        _resolver = resolver;
        IsInterfaceMemberMissing = false;
    }

    public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
    {
        if (typeDeclaration.ClassType == ClassType.Interface || typeDeclaration.ClassType == ClassType.Enum)
            return;
        base.VisitTypeDeclaration(typeDeclaration);
        var rr = _resolver.Resolve(typeDeclaration);
        if (rr.IsError)
            return;
        foreach (var baseType in typeDeclaration.BaseTypes)
        {
            var bt = _resolver.Resolve(baseType);
            if (bt.IsError || bt.Type.Kind != TypeKind.Interface)
                continue;
            bool interfaceMissing;
            var toImplement = ImplementInterfaceAction.CollectMembersToImplement(rr.Type.GetDefinition(), bt.Type, false, out interfaceMissing);
            if (toImplement.Count == 0)
                continue;

            IsInterfaceMemberMissing = true;
        }
    }
}

现在我们必须读取所有文件,解析它们并以下列方式调用上面的类:

var solutionFiles = new List<FileInfo>();
var trees = new Dictionary<FileInfo, SyntaxTree>();
IProjectContent projectContent = new CSharpProjectContent();
foreach (var file in solutionFiles.Where(f => f.Extension == ".cs").Distinct())
{
    var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();
    SyntaxTree syntaxTree;
    using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan))
    {
        syntaxTree = parser.Parse(fs, file.FullName);
    }
    trees.Add(file, syntaxTree);
    var unresolvedFile = syntaxTree.ToTypeSystem();
    projectContent = projectContent.AddOrUpdateFiles(unresolvedFile);
}
var compilation = projectContent.CreateCompilation();

foreach (var sharpFile in trees)
{
    var originalResolver = new CSharpAstResolver(compilation, sharpFile.Value, sharpFile.Value.ToTypeSystem());
    var visitor = new MissingInterfaceMemberImplementationVisitor(originalResolver);
    sharpFile.Value.AcceptVisitor(visitor);
    if (visitor.IsInterfaceMemberMissing)
       return false;
    }
return true;

答案 2 :(得分:2)

或者,您也可以使用as关键字。

class MyClass {  }
interface IInterface {  }

MyClass instance = new MyClass();
var inst = instance as IInterface;
if (inst != null)
{
    // your class implements the interface
}

如果您之后需要使用该实例而无需其他参考输入,请使用此选项。使用is仍然需要

if (instance is IInterface) 
{
    var inst = (IInterface) instance;
}

(instance as IInterface).InstanceProperty

所以为什么不马上做。