C# 中的通用值对象
Generic Value Object in C#
我有一个 VO class,其中包含几个变量,包括。一个可以是不同类型的变量,以防止以后强制转换我想知道我是否可以使它 class 通用。
public class InputVO<T>
{
public bool isEnabled;
public T value;
}
然后我想创建一个 InputVO 数组和一个获取类型化 InputVO 的方法...
public InputVO[] Inputs { get; private set; }
public InputVO GetInput(InputType type)
{
return Inputs[(int)type];
}
如何着手定义数组和 GetInput 方法,以便它们与通用 InputVO 一起使用? (InputType type
参数是一个枚举。我认为这里应该无关紧要)。
泛型类型参数在编译时是固定的。
每当你使用InputVO时,都需要填写那个类型参数。
public InputVO<T1>[] Inputs { get; private set; }
但您似乎想要的是每种数据类型的不同 InputVO 对象,并且能够在运行时按类型检索它们:
// Base class for all InputVOs
public abstract InputVOBase
{
public bool isEnabled;
}
// InputVO for a specific data-type
public class InputVO<T> : InputVOBase
{
public T Value;
}
现在您可以使用从 Type 到 InputVOBase 的字典。
// One InputVO per datatype
public Dictionary<Type, InputVOBase> AllInputs { get; private set; }
// Return the VO for type T, or null
public InputVO<T> GetInput<T>()
{
InputVOBase vo = AllInputs[typeof(T)];
return (vo as InputVO<T>);
}
稍微清理了解决方案。主要是你需要在字典中收集你的值。
void Main()
{
var a = new InputVO<string> { Value = "test" };
var b = new InputVO<int> { Value = 5 };
Inputs.Add(typeof(string), a);
Inputs.Add(typeof(int), b);
var x = GetInput<string>();
Console.WriteLine(x.Value);
var y = GetInput<int>();
Console.WriteLine(y.Value);
}
public abstract class InputVOBase
{
public bool isEnabled;
}
public class InputVO<T> : InputVOBase
{
public T Value;
}
public Dictionary<Type, InputVOBase> Inputs = new Dictionary<Type, InputVOBase>();
public InputVO<T> GetInput<T>()
{
return Inputs[typeof(T)] as InputVO<T>;
}
您不能在不指定类型的情况下创建泛型数组 class。但是,由于您可以控制基类型,因此可以使该实现成为一个非泛型接口并拥有一个集合:
//Empty interface
public interface IInputVO { }
//Your generic class now implements the interface
public class InputVO<T> : IInputVO
{
public bool isEnabled { get; set; }
public T Value { get; set; }
}
所以现在你的数组是接口类型 IInputVO
:
IInputVO[] inputs =
{
new InputVO<int>(),
new InputVO<string>(),
new InputVO<SomeClass>(),
};
感谢任何人的提示! ew,因为没有办法绕过转换,我只需要考虑几个类型,我认为所有基于泛型的解决方案对我来说都有点过分了。所以我只是在我的 VO 中添加了 casted getters ...
public class InputVO
{
public bool isEnabled;
public bool isValid;
public InputType type;
public object value;
public int IntValue { get { return (int)value; } }
public float FloatValue { get { return (float)value; } }
public bool BoolValue { get { return (bool)value; } }
public Vector2 Vector2Value { get { return (Vector2) value; } }
public Vector3 Vector3Value { get { return (Vector3)value; } }
}
我有一个 VO class,其中包含几个变量,包括。一个可以是不同类型的变量,以防止以后强制转换我想知道我是否可以使它 class 通用。
public class InputVO<T>
{
public bool isEnabled;
public T value;
}
然后我想创建一个 InputVO 数组和一个获取类型化 InputVO 的方法...
public InputVO[] Inputs { get; private set; }
public InputVO GetInput(InputType type)
{
return Inputs[(int)type];
}
如何着手定义数组和 GetInput 方法,以便它们与通用 InputVO 一起使用? (InputType type
参数是一个枚举。我认为这里应该无关紧要)。
泛型类型参数在编译时是固定的。
每当你使用InputVO时,都需要填写那个类型参数。
public InputVO<T1>[] Inputs { get; private set; }
但您似乎想要的是每种数据类型的不同 InputVO 对象,并且能够在运行时按类型检索它们:
// Base class for all InputVOs
public abstract InputVOBase
{
public bool isEnabled;
}
// InputVO for a specific data-type
public class InputVO<T> : InputVOBase
{
public T Value;
}
现在您可以使用从 Type 到 InputVOBase 的字典。
// One InputVO per datatype
public Dictionary<Type, InputVOBase> AllInputs { get; private set; }
// Return the VO for type T, or null
public InputVO<T> GetInput<T>()
{
InputVOBase vo = AllInputs[typeof(T)];
return (vo as InputVO<T>);
}
稍微清理了解决方案。主要是你需要在字典中收集你的值。
void Main()
{
var a = new InputVO<string> { Value = "test" };
var b = new InputVO<int> { Value = 5 };
Inputs.Add(typeof(string), a);
Inputs.Add(typeof(int), b);
var x = GetInput<string>();
Console.WriteLine(x.Value);
var y = GetInput<int>();
Console.WriteLine(y.Value);
}
public abstract class InputVOBase
{
public bool isEnabled;
}
public class InputVO<T> : InputVOBase
{
public T Value;
}
public Dictionary<Type, InputVOBase> Inputs = new Dictionary<Type, InputVOBase>();
public InputVO<T> GetInput<T>()
{
return Inputs[typeof(T)] as InputVO<T>;
}
您不能在不指定类型的情况下创建泛型数组 class。但是,由于您可以控制基类型,因此可以使该实现成为一个非泛型接口并拥有一个集合:
//Empty interface
public interface IInputVO { }
//Your generic class now implements the interface
public class InputVO<T> : IInputVO
{
public bool isEnabled { get; set; }
public T Value { get; set; }
}
所以现在你的数组是接口类型 IInputVO
:
IInputVO[] inputs =
{
new InputVO<int>(),
new InputVO<string>(),
new InputVO<SomeClass>(),
};
感谢任何人的提示! ew,因为没有办法绕过转换,我只需要考虑几个类型,我认为所有基于泛型的解决方案对我来说都有点过分了。所以我只是在我的 VO 中添加了 casted getters ...
public class InputVO
{
public bool isEnabled;
public bool isValid;
public InputType type;
public object value;
public int IntValue { get { return (int)value; } }
public float FloatValue { get { return (float)value; } }
public bool BoolValue { get { return (bool)value; } }
public Vector2 Vector2Value { get { return (Vector2) value; } }
public Vector3 Vector3Value { get { return (Vector3)value; } }
}