[英]Getting information from a private class inside a public method
I have the following.我有以下。
public static class Foo()
{
private class Bar()
{
public float x;
public float y;
public float z;
//... many more properties....
}
private static _bar = new Bar()
// Need other classes to be able to get x, y, z
}
I know it is a somewhat strange methodology, but the API I am interfacing with (Unity) requires that Bar() be an empty class (cannot inherit from anything and no functions, nor can it be a static class) of only public fields.我知道这是一种有点奇怪的方法,但我与 (Unity) 交互的 API 要求 Bar() 是一个只有公共字段的空类(不能从任何东西继承,也不能继承任何函数,也不能是静态类)。 For proper accessibility, I don't want other classes to be able to see this class as there is no need to create more than one.
为了获得适当的可访问性,我不希望其他类能够看到这个类,因为不需要创建多个类。 So, I put the class inside the static class that utilizes it and create a singleton of sorts (there are static functions that will be in this class that interact with the singleton).
所以,我把这个类放在使用它的静态类中并创建一个单例(这个类中有静态函数与单例交互)。
I will have situations where I need to access data from that singleton.我将遇到需要从该单例访问数据的情况。
I have tried a basic GetBar()
that returns _bar
but the compiler does not let me do this (which makes sense, other classes wouldn't understand the type being returned).我尝试了一个返回
_bar
的基本GetBar()
但编译器不允许我这样做(这是有道理的,其他类不会理解返回的类型)。 Thus, my only real solution is to make a bunch of GetX()
, GetY()
, etc. for every variable that I add to Bar().因此,我唯一真正的解决方案是为添加到 Bar() 的每个变量制作一堆
GetX()
、 GetY()
等。 This would work, but I would consider it poor practice if it can be avoided since now I need to make sure I manually add a Get() function every time I add a new property (which is messy).这会起作用,但如果可以避免,我会认为这是不好的做法,因为现在我需要确保每次添加新属性时手动添加 Get() 函数(这很混乱)。
I cannot make _bar public without making Bar() public and then any class could create an instance of Bar() which I don't want.如果不公开 Bar(),我无法公开 _bar,然后任何类都可以创建我不想要的 Bar() 实例。
Is there any way to automate getting the public properties of a private class that is within a public static class?有没有办法自动获取公共静态类中的私有类的公共属性?
This really strange architecture.这真是奇怪的建筑。 I'm not sure I understood the problem correctly, but I can offer a classic implementation of Singleton pattern:
我不确定我是否正确理解了这个问题,但我可以提供单例模式的经典实现:
public static class Foo
{
public class FooBar
{
// Will work
private float x = Bar.GetInstance().x;
// Will not work
private Bar bar = new Bar();
private float y = Bar.y;
}
}
public sealed class Bar
{
public float x;
public float y;
public float z;
private static Bar _instance;
// You can call the constructor only from Bar class
private Bar() { }
// Use this method to access Bar fields
public static Bar GetInstance()
{
// If Bar called first time it creates new instance
if (_instance == null) _instance = new Bar();
// If Bar instance was created use old instance
return _instance;
}
}
And about:关于:
Thus, my only real solution is to make a bunch of GetX(), GetY(), etc. for every variable that I add to Bar().
因此,我唯一真正的解决方案是为添加到 Bar() 的每个变量制作一堆 GetX()、GetY() 等。 This would work, but I would consider it poor practice if it can be avoided since now I need to make sure I manually add a Get() function every time I add a new property (which is messy).
这会起作用,但如果可以避免,我会认为这是不好的做法,因为现在我需要确保每次添加新属性时手动添加 Get() 函数(这很混乱)。
You can use auto-property like:您可以使用自动属性,例如:
public float X { get; set; }
This automatically create private field with accessors.这会自动创建带有访问器的私有字段。
If you want to conceal the implementation (class Bar
), but have an access to interface (ie x, y, z
) you can extract interface required:如果您想隐藏实现(类
Bar
),但可以访问接口(即x, y, z
),您可以提取所需的接口:
// Interface we want to have access to
public IBar {
float x {get; set;}
float y {get; set;}
float z {get; set;}
}
then然后
public static class Foo() {
// now Bar implements IBar
private class Bar : IBar {
// turn fields into properties which can be accessed via IBar interface
public float x {get; set;}
public float y {get; set;}
public float z {get; set;}
//... many more properties....
}
// Now other classes are able to get x, y, z:
// bar is public and it is of public interface IBar
public static readonly IBar bar = new Bar();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.