【问题标题】:How to fix compile error 'System.ServiceModel.ComIntegration.IParseDisplayName' is inaccessible due to its protection level'如何修复编译错误“System.ServiceModel.ComIntegration.IParseDisplayName”由于其保护级别而无法访问
【发布时间】:2017-08-23 06:03:26
【问题描述】:

我正在尝试在 C# 中实现一个 COM 组件,该组件可使用 GetObject 调用并提供自定义字符串。两个组件已经使用GetObject("winmgmts:\.\root\cimv2") 和使用GetObject("LDAP://example.com/OU=Users,DC=asp,DC=rippe,DC=com") 的LDAP 执行此WMI。我被这种自定义激活语法所吸引,并想复制它。

看来我必须实现 Class Com 接口IParseDisplayName

所以我尝试在 C# 中执行此操作,我有一个简单的 COM 对象,可以进行简单的计算。我一直在尝试实现 IParseDisplayName,我收到以下错误

`'System.ServiceModel.ComIntegration.IParseDisplayName' is inaccessible due to its protection level`

现在我看到了其他带有这些错误的 C# 问题,它们是访问修饰符问题,可以通过升级对 public 的访问来解决。但我不控制这个接口,因为它是一个系统接口。

请问如何解决?这是目前的代码。

using Microsoft.Win32;
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Xml;

namespace MonikerParseDisplayName
{
    // In Project Properties->Build->Check 'Interop for COM'
    // In AssemblyInfo.cs [assembly: ComVisible(true)]
    // compile with /unsafe In Project Properties->Build->Check 'Allow unsafe code'


    public interface ICalculator
    {
        double add( double a, double b);
        double mult(double a, double b);
    }
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(ICalculator))]
    //[System.Security.SuppressUnmanagedCodeSecurity]
    public class Calculator : ICalculator, System.ServiceModel.ComIntegration.IParseDisplayName  //, System.Runtime.InteropServices.ComTypes.IMoniker
    {
        public double add( double a, double b) 
        {
            return a+b;
        }
        public double mult(double a, double b)
        {
            return a*b;
        }

        //void IParseDisplayName.ParseDisplayName(IBindCtx pbc, IMoniker pmkToLeft, 
        //    string pszDisplayName, out int pchEaten, out IMoniker ppmkOut)
        void IParseDisplayName.ParseDisplayName(IBindCtx pbc, IMoniker pmkToLeft,
            string pszDisplayName, IntPtr pchEaten, IntPtr ppmkOut)
        {
            return new Exception("Not yet implemented");
        }
    }
}

编辑:另外,我认为 Windows Communication Foundation (WCF) 也使用相同的机制,这里是 link,这里是代码 sn-p。如果为真那么这证明它可以在 C# 中完成!

Set typedServiceMoniker = GetObject(  
"service4:address=http://localhost/ServiceModelSamples/service.svc,      binding=wsHttpBinding,   
contractType={9213C6D2-5A6F-3D26-839B-3BA9B82228D3}")  

【问题讨论】:

    标签: c# wcf com com-interop moniker


    【解决方案1】:

    您应该能够自己重新声明该接口。 .NET 中 COM 接口的有趣之处在于您可以在多个位置定义它们并且它们不会发生冲突。只需将其粘贴在代码中的某个位置并使用此定义而不是 System.ServiceModel.ComIntegration 中的定义即可。

    [ComImport]
    [SuppressUnmanagedCodeSecurity]
    [Guid("0000011a-0000-0000-C000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IParseDisplayName
    {
        void ParseDisplayName(IBindCtx pbc, [MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, IntPtr pchEaten, IntPtr ppmkOut);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2011-09-01
      • 1970-01-01
      相关资源
      最近更新 更多