引用对象数组名称作为属性
如何获取要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过不进行转换。您可以在 switch 语句中使用
string
,但不能使用object[]
。By not doing the conversion. You can use a
string
in a switch statement, but not anobject[]
.你不能,因为对象没有这样的“名称”。这甚至没有意义——许多变量可以保存对同一对象的引用。 “名字”应该是哪个?
想象一下,有一个名为 GetName() 的函数。在每种情况下程序会输出什么?
情况1:
情况2:
情况3:
情况4:
以此类推。
如果你想给你的对象“命名”,你必须自己做。也许创建一个“NamedObject”包装类,如下所示:
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:
Case 2:
Case 3:
Case 4:
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:
如果没有更多信息,我只能猜测
ConvertStringtoObjectArray
的作用。我的猜测是,它使用字符串作为键查找某些内容,然后返回一些值的数组。在大多数情况下,我希望原始字符串不再包含在对象数组中。因此,显然,你无法检索它,所以你必须“记住”它,即传递它。
例如,
您可以编写如下内容:
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
you could write something like this:
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.
如果“objEURUSD”的变量声明在您当前的范围内,您可以将您的参数与该变量进行比较,看看两者是否引用同一个对象...这不会查找某种名称,但也许这就是您想要的...
作为替代方案,您可以使用
Dictionary
或Dictionary
来存储数组,这样您可以查找给定名称的数组,反之亦然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...
as an alternative, you could use a
Dictionary<string,Object[]>
orDictionary<Object[],string>
to store your arrays, so you can lookup the array for a given name or vice versa