如何在C#中使用class属性? [英] How to use the class property in C#?
问题描述
我很擅长.NET并学习这些东西。我有一个来自相机库的类,它在我们调用下面的GetValue()方法时给出属性值。
I'm pretty new to .NET and learning the things. I have a class from a camera library which gives the property value when we call GetValue() method as below.
camera.Parameters[PLCamera.Width].GetValue();
我不确定这是如何工作的。两个方括号中给出的 PLCamera.Width
是枚举吗?本声明中基本上发生了什么?当我看到 PLCamera
类时,它有许多这样的属性。我想创建一个公共函数,它给出了作为参数给出的任何属性。
我尝试了什么:
我试过代码 camera.Parameters [PLCamera.Width] .GetValue();
并获得 PLCamera.Width
。我想知道这基本上是如何工作的?
CHM图书馆文档可以在 Pylon - 找到Google云端硬盘 [ ^ ]
I'm unsure how does that work. Is that PLCamera.Width
given in two square braces is an enum? What essentially happening in this statement? When I saw PLCamera
class, it has numerous such properties. I want to create a public function which gives any property given as argument.
What I have tried:
I have tried the code camera.Parameters[PLCamera.Width].GetValue();
and it gets the PLCamera.Width
. I want to know how this essentially works?
CHM library doc can be found at Pylon - Google Drive[^]
推荐答案
没有看源代码 - 我假设你可以做,但我没有下载 - 我会认为camera.Parameters是一个数组或一个字典,所以广场里面的值括号求值为整数(因此可以是枚举)或不同的类型,如字符串。
PLCamera不太可能是枚举,因为没有从枚举到int的隐式转换,所以它更可能是const int值的类/结构,或者参数是字典。
对于字典,它可能是这样的:
Without looking at the source code - which I assume you can do, but I don't which to download - I would assume that camera.Parameters is an array or a Dictionary, so the value inside the square brackets evaluates to an integer (and thus could be a enum) or a different type, like a string.
It's unlikely that PLCamera is an enum, as there is no implicit conversion from an enum to an int, so it's more likely that it's a class / struct of const int values, or Parameters is a Dictionary.
For a dictionary, it could be something like this:
public static class PLCamera
{
public static const string Width = "Width";
...
}
public class ParameterBlock
{
public int GetValue() { return 0; }
...
}
private Dictionary<string, ParameterBlock> Parameters = new Dictionary<string, ParameterBlock>();
...
int x = Parameters[PLCamera.Width].GetValue();
如果它是枚举,则可以这样做:
If it's an enum, then it could be done like this:
private Dictionary<PLCamera, ParameterBlock> Parameters = new Dictionary<PLCamera, ParameterBlock>();
...
int x = Parameters[PLCamera.Width].GetValue();
这篇关于如何在C#中使用class属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!