假设我想将Java中的以下实现转换为C#,这样我就不必在运行时强制转换返回的值,该转换应该已经在get方法中进行了处理。

如果您要问,为什么不创建setter和getters?仅仅是因为我计划拥有50-100个以上的属性,并且我不想为每个属性创建设置方法和获取方法。

[c#]-我最终想在c#中做什么

string name = playerA.getAttribute(Attribute.NAME);
int age = playerA.getAttribute(Attribute.AGE);


目前,除非将返回的值转换为正确的类型,否则我将无法执行。但是我可以在返回之前在get方法中进行转换吗?

[Java]-无论如何,这是当前的Java实现,无需强制转换

//setting attributes
playerA.setAttribute(Attribute.NAME, "Tom");
entityB.setAttribute(Attribute.AGE, 4);
...
//getting the attribute without casting
string name = playerA.getAttribute(PlayerAttribute.NAME);
int age = playerB.getAttribute(PlayerAttribute.AGE);


像这样设置播放器/实体内部的方法以获得属性
[Java]

public <E> E getAttribute(Attribute attr){
    //atrributeRepository is EnumMap<Attribute, Object>
    //how I will get my attribute value type at runtime
    Object result = attributeRepositoryMap.get(attr);

    //the enumMap will only ever hold these three type for this example
    if(result instanceof Boolean){ return (E) (Boolean) result; }
    if(result instanceof String){ return (E) (String) result; }
    if(result instanceof Integer){ return (E) (Integer) result; }

    return null;
    //say all checks are in place and null will never be reach
}


我能在C#中获得的最接近的是这个。

[c#]-尽管我可以处理,但我想防止强制转换

string name = (string) getAttribute<string>(Attribute.NAME);
int age = (int) getAttribute<int>(Attribute.AGE);


方法

public T getAttribute<T>(Attribute attribute){
{
     Object result = attributeRepositoryDictionary[attribute];
     return (T)result;
}


这是我在使用c#时可以获得属性的最接近的方法吗?

最佳答案

我不确定我是否真的喜欢这个主意-但您可以通过将Attribute设为通用来实现:

public static class Attributes
{
    public static Attribute<int> Age = new Attribute<int>("age");
    public static Attribute<string> Name = new Attribute<string>("name");
}

public class Attribute<T>
{
    public string Key { get; }

    public Attribute(string key)
    {
        Key = key;
    }

    ...
}


然后可以将您的方法实现为:

public T GetAttribute<T>(Attribute<T> attribute)
{
    // attributeDictionary would be a Dictionary<string, object>
    return (T) attributeDictionary[attribute.Key];
}


届时,类型推断将是您的朋友,因此您可以编写:

int a = player1.GetAttribute(Attributes.Age);


它等效于:

int a = player1.GetAttribute<int>(Attributes.Age);

08-05 04:45
查看更多