【问题标题】:struct inaccessible due to its protection level由于其保护级别而无法访问的结构
【发布时间】:2019-04-21 02:55:05
【问题描述】:

我在一个类中声明了一个私有结构。

当我尝试使用它时,编译器会引发错误

struct inaccessible due to its protection level

这是 C# 代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class HUDanimator : MonoBehaviour
{
    private struct udtThis
    {
        Color col1;
        Color col2;
        float wait;
        float fade;
    }

    private udtThis[] m = new udtThis[2];

    void Start()
    {
        udtThis n; //raises the compiler error
        n.wait = 0f; 

我在这里做错了什么?

谢谢。

【问题讨论】:

标签: c# class unity3d struct


【解决方案1】:

您的编译器很可能在n.wait = 0f; 行抱怨,因为结构的字段是私有的。将它们公开:

private struct udtThis
{
    public Color col1;
    public Color col2;
    public float wait;
    float fade;
}

然后你的代码示例编译就好了。

【讨论】:

    【解决方案2】:

    您可以在结构体publicinternal 中创建属性并以正常方式访问它们。

    我建议像这样封装它们:

        public Color Col1 { get; set; }
        public Color Col2 { get; set; }
        public float Wait { get; set; }
        public float Fade { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2011-09-01
      • 1970-01-01
      • 2011-04-02
      • 2011-02-07
      • 2012-01-25
      • 2022-01-18
      相关资源
      最近更新 更多