引用对象数组名称作为属性

发布于 2024-11-05 12:00:45 字数 875 浏览 0 评论 0原文

如何获取要在 SWITCH / CASE 函数中使用的对象数组的属性名称,如下所示?对于如何执行此操作的任何其他建议,我们将不胜感激。

Object[] objEURUSD = ConvertStringtoObjectArray(string val1);

Object[] objAUDUSD = ConvertStringtoObjectArray(string val2);

Object[] objGBPUSD = ConvertStringtoObjectArray(string val3);

示例函数使用

Object[] mvavgEURUSD = mvavgObject(objEURUSD);


private Object[] mvavgObject(Object[] val)
{

    string sym = val.ToString()  // this does not return the name it returns 'System.Object'

    switch (sym)
    {
        case "objEURUSD":

            // do something
            break;

        case "objAUDUSD":

            // do something
            break;

        case "objGBPUSD":
            // do something
            break;

    }
}

我可以在对象数组本身中包含对象“EURUSD”的名称,但它已经包含在对象的名称中。我只是不知道如何引用对象的名称。我不知道或者我不熟悉反射。

我感谢您对此事的帮助或建议。

How do I get the property name of an object array to use in a SWITCH / CASE function as follows? Any other suggestions on how to do this are appreciated.

Object[] objEURUSD = ConvertStringtoObjectArray(string val1);

Object[] objAUDUSD = ConvertStringtoObjectArray(string val2);

Object[] objGBPUSD = ConvertStringtoObjectArray(string val3);

Example Function Use

Object[] mvavgEURUSD = mvavgObject(objEURUSD);


private Object[] mvavgObject(Object[] val)
{

    string sym = val.ToString()  // this does not return the name it returns 'System.Object'

    switch (sym)
    {
        case "objEURUSD":

            // do something
            break;

        case "objAUDUSD":

            // do something
            break;

        case "objGBPUSD":
            // do something
            break;

    }
}

I could include the name of the object 'EURUSD' in the Object Array itself, but it's already inlcuded in the name of the object.. I just can't figure out how to reference the name of the object.. either I don't know or I am not conversant with Reflection.

I appreciate your help or suggestions on the matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦中楼上月下 2024-11-12 12:00:45

如何获取要在 SWITCH / CASE 函数中使用的对象数组的属性名称?

通过进行转换。您可以在 switch 语句中使用string,但不能使用object[]

How do I get the property name of an object array to use in a SWITCH / CASE function ?

By not doing the conversion. You can use a string in a switch statement, but not an object[].

暮色兮凉城 2024-11-12 12:00:45

你不能,因为对象没有这样的“名称”。这甚至没有意义——许多变量可以保存对同一对象的引用。 “名字”应该是哪个?

想象一下,有一个名为 GetName() 的函数。在每种情况下程序会输出什么?

情况1:

var a = new MyObject();
var b = a;
Console.WriteLine(GetName(b)); // Is it "a" or "b"?

情况2:

Console.WriteLine(GetName(new MyObject())); // What is the name now?

情况3:

Console.WriteLine(GetName(null)); // I guess you can return null here, but see case 4:

情况4:

MyObject a = null;
Console.WriteLine(GetName(a)); // Should this be "a" or null?

以此类推。

如果你想给你的对象“命名”,你必须自己做。也许创建一个“NamedObject”包装类,如下所示:

class NamedObject<T>
{
    public readonly T Object;
    public readonly string Name;

    public NamedObject(string name, string value)
    {
        this.Object = value;
        this.Name = name;
    }
}

You can't because there is no such "name" for an object. That wouldn't even make sense - many variables can hold a reference to the same object. Which one should be the "name"?

Imagine for a moment that there would be such a function called GetName(). What would the program output in each of these cases?

Case 1:

var a = new MyObject();
var b = a;
Console.WriteLine(GetName(b)); // Is it "a" or "b"?

Case 2:

Console.WriteLine(GetName(new MyObject())); // What is the name now?

Case 3:

Console.WriteLine(GetName(null)); // I guess you can return null here, but see case 4:

Case 4:

MyObject a = null;
Console.WriteLine(GetName(a)); // Should this be "a" or null?

And so on and so forth.

If you want to give your objects "names", you'll have to do so yourself. Perhaps create a "NamedObject" wrapper class like this:

class NamedObject<T>
{
    public readonly T Object;
    public readonly string Name;

    public NamedObject(string name, string value)
    {
        this.Object = value;
        this.Name = name;
    }
}
只为守护你 2024-11-12 12:00:45

如果没有更多信息,我只能猜测 ConvertStringtoObjectArray 的作用。我的猜测是,它使用字符串作为键查找某些内容,然后返回一些值的数组。

在大多数情况下,我希望原始字符串不再包含在对象数组中。因此,显然,你无法检索它,所以你必须“记住”它,即传递它。

例如,

Object[] objEURUSD = ConvertStringtoObjectArray(myVal);
Object[] mvavgEURUSD = mvavgObject(objEURUSD);

private Object[] mvavgObject(Object[] val)
{
    // ...

您可以编写如下内容:

Object[] mvavgEURUSD = mvavgObject(myVal);

private Object[] mvavgObject(string val)
{
    Object[] objEURUSD = ConvertStringtoObjectArray(val);

    // The string is in the parameter ‘val’, so we can use it here
    switch (val)
    {
        // ...
    }
}

Without further information, I can only guess what ConvertStringtoObjectArray does. My guess is that it looks something up using the string as a key, and then returns an array of some values.

In most cases, I would expect that the original string is no longer contained in the object array. Therefore, obviously, you cannot retrieve it, so you will have to “remember” it, i.e. pass it around.

For example, instead of

Object[] objEURUSD = ConvertStringtoObjectArray(myVal);
Object[] mvavgEURUSD = mvavgObject(objEURUSD);

private Object[] mvavgObject(Object[] val)
{
    // ...

you could write something like this:

Object[] mvavgEURUSD = mvavgObject(myVal);

private Object[] mvavgObject(string val)
{
    Object[] objEURUSD = ConvertStringtoObjectArray(val);

    // The string is in the parameter ‘val’, so we can use it here
    switch (val)
    {
        // ...
    }
}
戏蝶舞 2024-11-12 12:00:45

ToString() 的默认行为是返回类名。这可以在任何类中被重写,因为所有类都继承自对象。 string.ToString() 是您正在寻找的行为,因此您可能需要首先将对象转换回字符串(尽管数组部分很棘手,因为我不知道 ConvertStringtoObjectArray 方法的作用),但您需要执行投射以访问您正在寻找的内容。

我假设 object[] 是 char[]?如果是这样,转换应该像将对象转换为 char[] 然后转换为字符串一样简单。

the default behavior of ToString() is to return the class name. This can be overridden in any class as all classes inherit from object. string.ToString() is the behavior you are looking for so you probably need to first cast the object back to string (the array part is tricky though since I don't know what the ConvertStringtoObjectArray method does), but you need to do a cast to access what you are looking for.

I assume that the object[] is a char[]? if so the cast should be as easy as casting the object to a char[] and then to the string.

各空 2024-11-12 12:00:45

如果“objEURUSD”的变量声明在您当前的范围内,您可以将您的参数与该变量进行比较,看看两者是否引用同一个对象...这不会查找某种名称,但也许这就是您想要的...

Object[] objEURUSD; //initialized somewhere else


private Object[] mvavgObject(Object[] val)
{
   if(val==objEURUSD)
   {
      //it's the array referenced by objEURUSD ...
   }
}

作为替代方案,您可以使用 DictionaryDictionary 来存储数组,这样您可以查找给定名称的数组,反之亦然

if the variable declaration of "objEURUSD" is in your current scope, you could compare your parameter to that variable to see if both are referencing the same object ... this won't lookup some sort of name, but maybe it's what you want...

Object[] objEURUSD; //initialized somewhere else


private Object[] mvavgObject(Object[] val)
{
   if(val==objEURUSD)
   {
      //it's the array referenced by objEURUSD ...
   }
}

as an alternative, you could use a Dictionary<string,Object[]> or Dictionary<Object[],string> to store your arrays, so you can lookup the array for a given name or vice versa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文