C# 使用CodeFunction或CodeFunction2为需要此关键字的扩展方法添加参数

C# 使用CodeFunction或CodeFunction2为需要此关键字的扩展方法添加参数,c#,visual-studio-2012,extension-methods,envdte,C#,Visual Studio 2012,Extension Methods,Envdte,我正在使用VisualStudioCodeAutomation生成一些自定义代码和类 以下函数用于生成扩展方法 private void AddToPocoExtensionMethod(CodeClass2 entityAdapterClass, string name, string modifiedNameCamelCase, List<CodeProperty> properties) { var toPoco = (CodeFunction2)entityAdapt

我正在使用VisualStudioCodeAutomation生成一些自定义代码和类

以下函数用于生成扩展方法

private void AddToPocoExtensionMethod(CodeClass2 entityAdapterClass, string name, string modifiedNameCamelCase, List<CodeProperty> properties)
{
    var toPoco = (CodeFunction2)entityAdapterClass.AddFunction("ToPoco", vsCMFunction.vsCMFunctionFunction, "I" + name + "Poco", -1, vsCMAccess.vsCMAccessPublic, null);
    toPoco.IsShared = true;
    toPoco.AddParameter(modifiedNameCamelCase + "Entity", "I" + name + "Entity");
}
根据,AddParameter的语法为

CodeParameter AddParameter(
string Name,
Object Type,
Object Position
)

但是,语法不允许在参数类型之前添加“this”关键字


我是否应该使用其他Api创建一个方法作为扩展方法?如果可能,请共享解决方案。

不幸的是,代码模型无法做到这一点。它不支持为扩展方法添加必要的语法

您需要向CodeFunction添加一个“ExtensionMethod”扩展程序。检查并确认。例如,这是检查方法是否为扩展的方式:

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("f82170cc-efe8-4f5e-8209-bc2c27b3f54d")]
internal interface ICSExtensionMethodExtender
{
    bool IsExtension { get; }
}

private bool IsExtensionMethod(CodeFunction codeFunction)
{
    ICSExtensionMethodExtender extender = codeFunction.Extender["ExtensionMethod"] as ICSExtensionMethodExtender;
    return codeFunction.IsShared && extender != null && extender.IsExtension;
}
这未经测试,但您可以尝试以下方法:

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("f82170cc-efe8-4f5e-8209-bc2c27b3f54d")]
internal interface ICSExtensionMethodExtender 
{
    bool IsExtension { get; }
}

[ComVisible(true)]
[ComDefaultInterface(typeof(ICSExtensionMethodExtender))]
public class ExtensionMethodExtender : ICSExtensionMethodExtender
{
    internal static ICSExtensionMethodExtender Create(bool isExtension)
    {
        var result = new ExtensionMethodExtender(isExtension);
        return (ICSExtensionMethodExtender)ComAggregate.CreateAggregatedObject(result);
    }

    private readonly bool _isExtension;

    private ExtensionMethodExtender(bool isExtension)
    {
        _isExtension = isExtension;
    }

    public bool IsExtension
    {
        get { return _isExtension; }
    }
}

private void SetExtension(CodeFunction codeFunction)
{
    ICSExtensionMethodExtender extender = new ExtensionMethodExtender(true);
    codeFunction.Extender["ExtensionMethod"] = extender;
    codeFunction.IsShared = true;
}

我来晚了。但其他人可能需要这个答案:)


谢谢分享。你能分享一些我如何使用这个类或接口来添加扩展方法的例子吗?
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("f82170cc-efe8-4f5e-8209-bc2c27b3f54d")]
internal interface ICSExtensionMethodExtender
{
    bool IsExtension { get; }
}

private bool IsExtensionMethod(CodeFunction codeFunction)
{
    ICSExtensionMethodExtender extender = codeFunction.Extender["ExtensionMethod"] as ICSExtensionMethodExtender;
    return codeFunction.IsShared && extender != null && extender.IsExtension;
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("f82170cc-efe8-4f5e-8209-bc2c27b3f54d")]
internal interface ICSExtensionMethodExtender 
{
    bool IsExtension { get; }
}

[ComVisible(true)]
[ComDefaultInterface(typeof(ICSExtensionMethodExtender))]
public class ExtensionMethodExtender : ICSExtensionMethodExtender
{
    internal static ICSExtensionMethodExtender Create(bool isExtension)
    {
        var result = new ExtensionMethodExtender(isExtension);
        return (ICSExtensionMethodExtender)ComAggregate.CreateAggregatedObject(result);
    }

    private readonly bool _isExtension;

    private ExtensionMethodExtender(bool isExtension)
    {
        _isExtension = isExtension;
    }

    public bool IsExtension
    {
        get { return _isExtension; }
    }
}

private void SetExtension(CodeFunction codeFunction)
{
    ICSExtensionMethodExtender extender = new ExtensionMethodExtender(true);
    codeFunction.Extender["ExtensionMethod"] = extender;
    codeFunction.IsShared = true;
}
yourAddedParameter.StartPoint.CreateEditPoint().Insert("this ");