How can I use EnvDTE within a .NET Standard projec

2019-02-26 10:13发布

问题:

I'm porting a .net 4.7 project to .net standard, which uses T4 to generate some code. I iterate over the classes, properties and methods. This use to work perfectly, but now the return types of DTE are of type 'System.__ComObject' and I cannot cast them.

Error: Unable to cast COM object of type 'System.__ComObject' to interface type 'EnvDTE.CodeClass'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B1F42514-91CD-4D3A-8B25-A317D8032B24}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Any ideas how to fix this?

Example:

public static bool CheckBase(CodeClass codeClass, string baseName)
{
    var bases = codeClass.Bases;
    if (bases != null)
    {
        foreach (CodeClass baseClass in bases)
        {
            if(baseClass.Name == baseName) return true;
        }
    }
    return false;
}