3

我想创建一个 resharper 插件,它从类中删除具有返回类型字符串的属性。我已经创建了一个IActionHandler从所选类中获取所有属性的方法,但我不知道如何修改代码结构以从类中删除属性

这是 Execute 的方法IActionHandler

public void Execute(IDataContext context, DelegateExecute nextExecute)
{
    // Fetch active solution from context.
    ISolution solution = context.GetData(JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION);
    if (solution == null)
        return;

    var declaredElements = context.GetData(DataConstants.DECLARED_ELEMENTS);
    if (declaredElements == null || declaredElements.IsEmpty())
        return;

    IDeclaredElement declaredElement = declaredElements.First();

    var classElement = declaredElement as IClass;
    if (classElement != null)
    {
        var properties = classElement.Properties.Where(p => p.Type.IsString());

        foreach (IProperty property in properties)
        {
            // Remove IProperty from IClass   <--
        }
    }
}

有任何想法吗?

4

1 回答 1

1

我找到了答案:

public void Execute(IDataContext context, DelegateExecute nextExecute)
{
    // Fetch active solution from context.
    ISolution solution = context.GetData(JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION);
    if (solution == null)
        return;

    var declaredElements = context.GetData(DataConstants.DECLARED_ELEMENTS);
    if (declaredElements == null || declaredElements.IsEmpty())
        return;

    IDeclaredElement declaredElement = declaredElements.First();

    var classElement = declaredElement as IClass;
    if (classElement != null)
    {
        // As a class can be declared in multiple files (partial classes) we enumerate all
        // declarations and choose the first one
        var declarations = classElement.GetDeclarations();
        var classDeclaration = declarations.First() as IClassDeclaration;

        var properties = classDeclaration.PropertyDeclarations.Where(p => p.Type.IsString());

        foreach (var propertyDeclaration in properties)
        {       
            PsiManager.GetInstance(solution).DoTransaction(() =>
            {
                classDeclaration.RemoveClassMemberDeclaration(propertyDeclaration);
            },
            "PsiTransactionCommand");
        }
    }
}
于 2013-08-08T10:55:46.080 回答