函数是否可以返回未知类型的泛型类的实例
本文关键字:类型 泛型类 实例 未知 返回 是否 函数 | 更新日期: 2023-09-27 18:34:43
我有一个名为ImageProperty的实用程序类,用于存储特定图像属性的类型和值。
键入只能是以下枚举值之一:
public enum ImagePropertyType { SIZE, H_RES, V_RES, BIT_COUNT, IS_ALPHA }
这些值都是不同类型的(大小、浮点数、浮点数、整数、布尔值(。
我的 ImageProperty 类的简化形式如下:
public class ImageProperty
{
private ImagePropertyType type;
private object value;
public ImageProperty(ImagePropertyType type, object value)
{
this.type = type;
this.value = value;
}
public ImagePropertyType getType()
{
return this.type;
}
public void setType(ImagePropertyType type)
{
this.type = type
}
public object getValue()
{
return this.value;
}
public void setValue(object value)
{
this.value = value;
}
}
请注意使用 object 来获取/设置值(因为类型不同(。
我想让我的类通用,因为我不喜欢使用 object,所以我对类和方法进行了一些更改:
public class ImageProperty<T>
{
...
private T value;
...
public ImageProperty(ImagePropertyType type, T value)
...
public T getValue()
...
public void setValue(T value)
...
}
我在另一个类中有一个函数,需要根据给定的类型返回 ImageProperty 的实例。
public ???? getImageProperty(ImagePropertyType type, Bitmap bitMap)
{
switch(type)
{
case SIZE:
return new ImageProperty<Size>(type, bitMap.Size);
case H_RES:
return new ImageProperty<float>(type, bitMap.HorizontalResolution);
case V_RES:
return new ImageProperty<float>(type, bitMap.VerticalResolution);
...
...
}
}
我不确定为此方法放置什么返回类型(因此????(。
我不能只是说:
public ImageProperty getImageProperty(ImagePropertyType type, Bitmap bitMap)
因为图像属性类需要参数化。
显然,如果值类型始终是,比如说 int,我会将返回类型设置为:
public ImageProperty<int> getImageProperty(ImagePropertyType type, Bitmap bitMap)
有没有办法将getImageProperty的返回类型定义为"任何或未知的参数化值"?
像这样:
public ImageProperty<?> getImageProperty(ImagePropertyType type, Bitmap bitMap)
参数化类不是一个好主意,因为我不知道要返回的值的类型?
我是否应该只使 ImageProperty 类成为非通用类(就像我帖子中的第一个类(并返回使用对象作为返回类型,如果我需要知道值类型,我可以使用 typeof 获取它?
object value = getImageProperty(ImagePropertyType.SIZE, Bitmap bitMap).getValue();
Type t = typeof(value);
谢谢。
----更新---------------------------------
根据Knaģis的建议和进一步的阅读,我决定保留原始类,然后创建一个扩展ImageProperty的泛型类:
public class ImageProperty<T> : ImageProperty
{
private T propertyValue;
public ImageProperty()
: base()
{
}
public ImageProperty(ImagePropertyType propertyType, T propertyValue)
: base(propertyType, propertyValue)
{
this.propertyValue = propertyValue;
}
public T getPropertyValue()
{
return propertyValue;
}
public void setPropertyValue(T propertyValue)
{
this.propertyValue = propertyValue;
}
}
然而,有一件事。我收到一个编译器警告,上面写着:
ImageProperty<T>.getPropertyValue() hides inherited member ImageProperty.getPropertyValue(). Use the new keyword if hiding was intended.
我添加了新关键字:
public new T getPropertyValue((
奇怪的是,我从来不知道方法声明中使用的新关键字以及如何使用它。有人愿意解释吗?
在您的情况下,您唯一能做的就是请求返回特定类型的方法:
public ImageProperty<T> getImageProperty<T>(ImagePropertyType type, Bitmap bitMap)
// call the method
ImageProperty<int> result = obj.getImageProperty<int>(ImagePropertyType.SIZE, bitmap).
调用方必须知道该类型T
,否则编译器将无法理解您的代码。如果您不知道它总是如此,那么您的泛型类必须从非泛型基类继承,然后该方法将返回稍后可以强制转换为特定泛型实现的非泛型类型。
// class
public class ImageProperty<T> : ImageProperty {}
// and methods that can be used (you can only use one of these unless you rename one)
public ImageProperty<T> getImageProperty<T>(ImagePropertyType type, Bitmap bitMap)
public ImageProperty getImageProperty(ImagePropertyType type, Bitmap bitMap)