gpt4 book ai didi

c# - 使用 Resharper 7.1 插件从 IClass 中删除 IProperty

转载 作者:行者123 更新时间:2023-11-30 17:52:47 31 4
gpt4 key购买 nike

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

这是 IActionHandler 的 Execute 方法:

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 <--
}
}
}

有什么想法吗?

最佳答案

我找到了答案:

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");
}
}
}

关于c# - 使用 Resharper 7.1 插件从 IClass 中删除 IProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121126/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com