“公共结构包含将要导出的一个或多个非公共字段”。 -但是没有字段吗? [英] "The public struct contains one or more non-public fields that will be exported" - but there are no fields?

查看:120
本文介绍了“公共结构包含将要导出的一个或多个非公共字段”。 -但是没有字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的结构正在针对COM互操作进行编译。我收到以下构建警告:


warning:类型库导出程序警告处理
'MyNamespace.MyStruct.k__BackingField,
MyAssemblyName。 警告:公共结构包含一个
或将要导出的多个非公共字段


但是我不明白它指的是什么-根本没有非公共字段,也没有字段。也许编译器生成了我看不到的东西?该警告是什么意思,我能做些什么来清理它?



以下是正在构建的(经过稍微清理)的代码:

  [Guid( ....)] 
[ComVisible(true)]
公共结构MyStruct
{
公用字符串StringA {get;组; }
公共字符串StringB {get;组; }

public MyStruct(string a,string b)
{
StringA = a;
StringB = b;
}

public MyStruct(MyStruct other)
{
StringA = other.StringA;
StringB = other.StringB;
}

公共重写bool Equals(object obj)
{
if(!(obj is MyStruct))return false;
var other =(MyStruct)obj;
返回
other.StringA == this.StringA&
other.StringB == this.StringB;
}

公共静态布尔运算符==(MyStructa,MyStructb)=> != null&& a。等于(b);
公共静态布尔运算符!=(MyStructa,MyStructb)=> !(a == b);
公共重写int GetHashCode()=> ToString()。GetHashCode();

公共替代字符串ToString()=> $ {StringA}-{StringB};
}

在这里最好的是生成的IDL:

  typedef [uuid(....),版本(1.0),自定义(xxxx,MyNamespace.MyStruct)] 
struct tagMyStruct {
LPSTR< StringA> k__BackingField;
LPSTR< StringB> k__BackingField;
} MyStruct;由OleView生成的

。我可以看到它包含与警告中相同的 k__BackingField -但不清楚这意味着什么。

解决方案

您正在使用自动属性:

 公共字符串StringA {get;组; } 

编译器会为每个文件自动生成一个后备字段。

  MyNamespace.MyStruct.k__BackingField 


是指。






您收到警告是因为暴露了私有字段可能是意外的,或者可能会导致安全问题。由开发人员来验证是否是这种情况。在您的特定示例中,不会违反封装,因此可以忽略或取消警告。另请参阅 MSDN


何时禁止显示警告



如果可以接受该字段的公开曝光,则可以从此规则中禁止警告。



I have the struct below which is being compiled for COM interop. I get the following build warning:

warning : Type library exporter warning processing 'MyNamespace.MyStruct.k__BackingField, MyAssemblyName'. Warning: The public struct contains one or more non-public fields that will be exported.

But I don't see what it is referring to - there are no non-public fields nor fields at all. Maybe the compiler is generating something I can't see? What does this warning mean, and what if anything can I do to clean it up?

Here's the (slightly sanitized) code which is being built:

[Guid("....")]
[ComVisible(true)]
public struct MyStruct
{
    public string StringA { get; set; }
    public string StringB { get; set; }

    public MyStruct(string a, string b)
    {
        StringA = a;
        StringB = b;
    }

    public MyStruct(MyStruct other)
    {
        StringA = other.StringA;
        StringB = other.StringB;
    }

    public override bool Equals(object obj)
    {
        if (!(obj is MyStruct)) return false;
        var other = (MyStruct)obj;
        return
            other.StringA == this.StringA &&
            other.StringB == this.StringB;
    }

    public static bool operator ==(MyStructa, MyStructb) => a != null && a.Equals(b);
    public static bool operator !=(MyStructa, MyStructb) => !(a == b);
    public override int GetHashCode() => ToString().GetHashCode();

    public override string ToString() => $"{StringA}-{StringB}";
}

and for good measure here is the IDL that gets generated:

typedef [uuid(....), version(1.0), custom(xxxx, MyNamespace.MyStruct)]
struct tagMyStruct {
    LPSTR <StringA>k__BackingField;
    LPSTR <StringB>k__BackingField;
} MyStruct;

as generated by OleView. I can see it contains the same k__BackingField as in the warning - but its not clear what this means.

解决方案

You are using auto-properties:

public string StringA { get; set; }

The compiler auto-generates a backing field for each of them. This is exactly what

MyNamespace.MyStruct.k__BackingField

is referring to.


You are getting the warning because exposing a private field could be unintended or could lead to security issues. It is up to the developer to verify if that is the case. In your specific example, there would be no breach of encapsulation so it's fine to ignore the warning or suppress it. See also the official documentation on this from MSDN:

When to suppress warnings

It is safe to suppress a warning from this rule if public exposure of the field is acceptable.

这篇关于“公共结构包含将要导出的一个或多个非公共字段”。 -但是没有字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆