在枚举运算符的帮助下实现属性 [英] Implementing properties with help of enum operator

查看:117
本文介绍了在枚举运算符的帮助下实现属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,所以也许我的问题对某些人来说似乎太幼稚了.我有一个名为config的类,其中一个名为kye的字符串字段.

当我应用该类的GET属性时,该属性必须返回一个不同类型(Int或bool或String)的变量kye.

我需要在枚举运算符的帮助下实现这一点.请知道我该怎么做吗?

I''m a newer in c# so maybe my question will seem naive to some of you. I have a class named config with one string field named kye.

When I apply the GET property of the class, the property has to return one variable kye in different types (Int or bool or String).

I need to implement this with help of enum operator. Please,any idea how can I do this?

推荐答案

我认为您需要退后一两步,看看enum到底是什么.

C#中没有枚举运算符"-enum是值和值名称的列表.

我不太确定您要使用哪种枚举,但是我怀疑您在错误的树上吠叫!
您想实现什么,您认为这是一个很好的解决方案?
I think you need to go back a step or two, and look at what an enum actually is.

There is no "enum operator" in C# - an enum is a list of values and names for values.

I''m not quite sure what it is you are trying to use an enum for, but I suspect that you are barking up to wrong tree!
What are you trying to achieve, that you think this is a good solution?


您的意思是这样的吗?

You mean something like this?

public enum RetType {RetInt, RetBool, RetString};
...
public object PolimorphProperty(string key, RetType how) 
{
    get 
    { 
       switch (how)
       {
           case RetType.RetInt:
              ...;
           case RetType.RetBool:
              ...;
           case RetType.RetString:
              ...;
       }
    }
}



您可以返回对象,但这无济于事,因为在调用方,您仍然需要将其抛回.您需要在呼叫方知道所需的类型.

您应该改为实现三种不同的getter方法.



You can return object, but this won''t help you, since on caller side you will need to cast it back anyway. You need to know on caller side what type you expect.

You should implement three different getter methods instead.


这篇关于在枚举运算符的帮助下实现属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆