通用接口和运行时对象的转换问题 [英] Generic Interfaces and casting from object at runtime problem
问题描述
让我们说我想要一个支持Min / Max
属性的泛型类型,如果需要,可以是double或整数甚至是datetime,
灵活的东西。
所以我开始创建以下通用接口:
*注意:实际上我实现了IComparable和IEquatable泛型
接口和相关的覆盖方法,但是我已经将这个例子的所有内容减少到了最低限度。
public界面IMinMax< T>
{
T Min {get;}
T Max {get;}
} $ / $
和以下通用结构:
public struct MinMax< T:IMinMax< T>
{
私人读取T min;
私人读取T max;
public T Min
{
get
{
返回分钟;
}
}
public T Max
{
get
{
返回最大值;
}
}
公共MinMax(T min,T max)
{
this.min = min;
this.max = max;
}
}
现在这里有一些使用它的代码:
IMinMax< intintMinMax = new MinMax< int>( 0,100); //百分比范围
IMinMax< ddateMinMax = new MinMax< DateTime>(new DateTime(1973,10,
4),DateTime.Now); //日期范围
好的,所以这就是问题......如果我有以下
程序怎么办?
public void DoSomething(object o)
{
IMinMax< mm =(IMinMax<>)o; //这个不行吗
//用mm.Min和mm.Max做点什么
}
我想按如下方式调用该程序:
DoSomething(intMinMax);
DoSomething(dateMinMax);
我们如何与Min和Max做点什么?显然,代码中缺少了很多肉,而且我为了这个新闻组的目的而将其简化为非常简单的
所以请不要
问我为什么要这样做...这会以一种或另一种形式出现所有
时间。
我猜真实问题是......一旦你将一个通用界面
投射到一个对象上,你如何在run-
时间内提取它的信息呢?在我的情况下,我碰巧在运行时知道类型,但是修改后的方法后面的
仍然不起作用:
public void DoSomething(Type t,对象o)
{
IMinMax< tmm =(IMinMax< t>)o; //仍然无法工作
//用mm.Min和mm.Max做点什么
}
问候!
Anthony
Let''s say that I would like a generic type that supports Min/Max
properties and can be double or integer or even datetime if need be,
something flexible.
So I go about creating the following generic interface :
*note : in reality I implement the IComparable and IEquatable generic
interfaces and associated overriden methods, but I''ve cut everything
down to the bare minimum for this example.
public interface IMinMax<T>
{
T Min{get;}
T Max{get;}
}
and the following generic struct :
public struct MinMax<T: IMinMax<T>
{
private readonly T min;
private readonly T max;
public T Min
{
get
{
return min;
}
}
public T Max
{
get
{
return max;
}
}
public MinMax(T min, T max)
{
this.min = min;
this.max = max;
}
}
Now here''s some code to use it :
IMinMax<intintMinMax = new MinMax<int>(0, 100); // percentage range
IMinMax<ddateMinMax = new MinMax<DateTime>(new DateTime(1973, 10,
4), DateTime.Now); // date range
Okay, so here''s the problem... what if I have the following
procedure :
public void DoSomething(object o)
{
IMinMax<mm = (IMinMax<>) o; // this doesn''t work
// do something with mm.Min and mm.Max here
}
and I want to call the procedure as follows :
DoSomething(intMinMax);
DoSomething(dateMinMax);
How do we go about doing something with Min and Max? Obviously there''s
a lot of meat missing in the code and I simplified it quite
unrealistically for the purpose of this newsgroup so please no
questions as to why I would want to do it... this comes up all the
time in one form or another.
I guess the real question is... once you''ve cast a generic interface
to an object, how do you go about extracting its information at run-
time? In my case I happen to know the type at runtime but the
following modified method still doesn''t work :
public void DoSomething(Type t, object o)
{
IMinMax<tmm = (IMinMax<t>) o; // still doesn''t work
// do something with mm.Min and mm.Max here
}
Regards!
Anthony
推荐答案
你的结构(或价值类型)是盒装的类型信息似乎丢失
转换为对象时..
您可以尝试C ++ / CLI,它会在您输入值时保留类型信息
类型。
参考
C ++:Kenny最强大的.NET框架编程语言
Kerr,MSDN
-
盛江
VC ++中的微软MVP
" Anthony Paul" < a ********** @ gmail.comwrote in message
news:11 ******************** **@o11g2000prd.googlegr oups.com ...
Your struct (or value type) is boxed and the type information seems lost
when it is converted to object..
You can try C++/CLI, which retains type information when you box a value
type.
Reference
C++: The Most Powerful Language for .NET Framework Programming by Kenny
Kerr, MSDN
--
Sheng Jiang
Microsoft MVP in VC++
"Anthony Paul" <an**********@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
让我们说我想要一个支持Min / Max的通用类型
属性,如果需要可以是double或整数甚至是datetime,
灵活的东西。
所以我要创建以下通用接口:
*注意:实际上我实现了IComparable和IEquatable通用
接口和相关的覆盖方法,但我已经削减了一切
这个例子的最低限度。
公共接口IMinMax< T>
{
T Min { get;}
T Max {get;}
}
和以下通用结构:
public struct MinMax< T:IMinMax< T>
{
private readonly T min;
private readonly T max ;
public T Min
{
get
{
返回分钟;
}
}
public T Max
{
get
{
返回最大值;
}
}
公共MinMax(T min,T max)
{
this.min = min;
this.max = max;
}
}
现在这里有一些代码可以使用它:
IMinMax< intintMinMax = new MinMax< int>( 0,100); //百分比范围
IMinMax< ddateMinMax = new MinMax< DateTime>(new DateTime(1973,10,
4),DateTime.Now); //日期范围
好的,所以这就是问题......如果我有以下
程序怎么办?
public void DoSomething(object o)
{
IMinMax< mm =(IMinMax<>)o; //这个不行吗
//用mm.Min和mm.Max做点什么
}
我想按如下方式调用该程序:
DoSomething(intMinMax);
DoSomething(dateMinMax);
我们如何与Min和Max做点什么?显然,代码中缺少了很多肉,而且我为了这个新闻组的目的而将其简化为非常简单的
所以请不要
问我为什么要这样做...这会以一种或另一种形式出现所有
时间。
我猜真实问题是......一旦你将一个通用界面
投射到一个对象上,你如何在run-
时间内提取它的信息呢?在我的情况下,我碰巧在运行时知道类型,但是修改后的方法后面的
仍然不起作用:
public void DoSomething(Type t,对象o)
{
IMinMax< tmm =(IMinMax< t>)o; //仍然无法工作
//用mm.Min和mm.Max做点什么
}
问候!
Anthony
Let''s say that I would like a generic type that supports Min/Max
properties and can be double or integer or even datetime if need be,
something flexible.
So I go about creating the following generic interface :
*note : in reality I implement the IComparable and IEquatable generic
interfaces and associated overriden methods, but I''ve cut everything
down to the bare minimum for this example.
public interface IMinMax<T>
{
T Min{get;}
T Max{get;}
}
and the following generic struct :
public struct MinMax<T: IMinMax<T>
{
private readonly T min;
private readonly T max;
public T Min
{
get
{
return min;
}
}
public T Max
{
get
{
return max;
}
}
public MinMax(T min, T max)
{
this.min = min;
this.max = max;
}
}
Now here''s some code to use it :
IMinMax<intintMinMax = new MinMax<int>(0, 100); // percentage range
IMinMax<ddateMinMax = new MinMax<DateTime>(new DateTime(1973, 10,
4), DateTime.Now); // date range
Okay, so here''s the problem... what if I have the following
procedure :
public void DoSomething(object o)
{
IMinMax<mm = (IMinMax<>) o; // this doesn''t work
// do something with mm.Min and mm.Max here
}
and I want to call the procedure as follows :
DoSomething(intMinMax);
DoSomething(dateMinMax);
How do we go about doing something with Min and Max? Obviously there''s
a lot of meat missing in the code and I simplified it quite
unrealistically for the purpose of this newsgroup so please no
questions as to why I would want to do it... this comes up all the
time in one form or another.
I guess the real question is... once you''ve cast a generic interface
to an object, how do you go about extracting its information at run-
time? In my case I happen to know the type at runtime but the
following modified method still doesn''t work :
public void DoSomething(Type t, object o)
{
IMinMax<tmm = (IMinMax<t>) o; // still doesn''t work
// do something with mm.Min and mm.Max here
}
Regards!
Anthony
盛江[MVP]< sh *********@hotmail.com.discusswrote:
Sheng Jiang[MVP] <sh*********@hotmail.com.discusswrote:
您的结构(或值类型)已装箱且类型信息似乎丢失了
转换为对象时..
Your struct (or value type) is boxed and the type information seems lost
when it is converted to object..
不,当盒装信息时,类型信息肯定不会丢失。试试
取消装箱错误的类型 - 你会很快发现的。
-
Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet
如果回复小组,请不要给我发邮件
No, type information is certainly *not* lost when it''s boxed. Just try
unboxing something to the wrong type - you''ll find out soon enough.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
看来他没有指定泛型的类型
这个程序运行正常
static void Main(string [] args)
{
IMinMax< intintMinMax = new MinMax< int>(0,100); //百分比
范围
IMinMax< DateTimedateMinMax = new MinMax< DateTime>(新
DateTime(1973,10,
4),DateTime.Now); //日期范围
DoSomething< int>(intMinMax);
DoSomething< DateTime>(dateMinMax);
}
public static void DoSomething< T>(对象o)
{
IMinMax< Tmm =(IMinMax< T>)o;
//用mm.Min和mm.Max做点什么
}
-
Sheng Jiang
VC ++中的Microsoft MVP
" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言
新闻:MP ********************* @ msnews.microsoft.com 。 ..
It seems he did not specify the type for the generics
this program runs fine
static void Main(string[] args)
{
IMinMax<intintMinMax = new MinMax<int>(0, 100); // percentage
range
IMinMax<DateTimedateMinMax = new MinMax<DateTime>(new
DateTime(1973, 10,
4), DateTime.Now); // date range
DoSomething<int>(intMinMax);
DoSomething<DateTime>(dateMinMax);
}
public static void DoSomething<T>(object o)
{
IMinMax<Tmm = (IMinMax<T>) o;
// do something with mm.Min and mm.Max here
}
--
Sheng Jiang
Microsoft MVP in VC++
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
盛江[MVP]< sh ********* @ hotmail.com.discusswrote:
Sheng Jiang[MVP] <sh*********@hotmail.com.discusswrote:
您的结构(或值类型)被装箱,类型信息似乎丢失
转换为对象时..
Your struct (or value type) is boxed and the type information seems lost
when it is converted to object..
不,当盒装信息时,类型信息肯定不会丢失。试试
取消装箱错误的类型 - 你会很快发现的。
-
Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet
如果回复小组,请不要给我发邮件
No, type information is certainly *not* lost when it''s boxed. Just try
unboxing something to the wrong type - you''ll find out soon enough.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这篇关于通用接口和运行时对象的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!