返回对象引用的通用方法 [英] Generic method to return object reference
问题描述
我在< List>中有一个不同对象的列表结构体。每种物品只有一个类别。
目前我有以下几种方法:
public static Flag GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s是国旗)
返回(旗帜)s;
}
返回null;
}
public static Cloth GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s is Cloth)
return(Cloth)s;
}
返回null;
}
公共静态BedSprd GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s是BedSprd)
return(BedSprd)s;
}
返回null;
}
有没有办法我可以使用一个通用方法将
参数作为我希望返回的项目?
感谢您的帮助。
Al
Hi,
I have a list of different objects in a <List> Structure. There is only
one category of each kind of object.
Current I have the following methods:
public static Flag GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is Flag)
return (Flag)s;
}
return null;
}
public static Cloth GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is Cloth)
return (Cloth)s;
}
return null;
}
public static BedSprd GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is BedSprd)
return (BedSprd)s;
}
return null;
}
Is there a way I could have one generic method that would take as a
parameter the item I wish returned?
Thank you for your help.
Al
推荐答案
" acb" < CH ****** @ gmail.com> écritdansle message de news:
11**********************@g47g2000cwa.googlegroups。 com ...
|我在< List>中有一个不同对象的列表。结构体。只有
|每种物体的一类。
|
|目前我有以下方法:
|
| public static Flag GetFlagObj()
| {
| foreach(Thingy s in _Thingies)
| {
| if(s是Flag)
| return(Flag)s;
| }
|返回null;
| }
|
| public static Cloth GetFlagObj()
| {
| foreach(Thingy s in _Thingies)
| {
| if(s是Cloth)
|返回(布料)s;
| }
|返回null;
| }
|
| public static BedSprd GetFlagObj()
| {
| foreach(Thingy s in _Thingies)
| {
| if(s是BedSprd)
|返回(BedSprd)s;
| }
|返回null;
| }
|
|
|有没有办法我可以有一个通用方法,可以作为一个
|参数我希望退回吗?
是的,你是这样做的:
公共静态T GetObj< T>()
{
foreach(Thingy s in _Thingies)
{
if(s is T)
返回(T)s;
}
返回默认值(T);
}
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
"acb" <ch******@gmail.com> a écrit dans le message de news:
11**********************@g47g2000cwa.googlegroups. com...
| I have a list of different objects in a <List> Structure. There is only
| one category of each kind of object.
|
| Current I have the following methods:
|
| public static Flag GetFlagObj()
| {
| foreach (Thingy s in _Thingies)
| {
| if (s is Flag)
| return (Flag)s;
| }
| return null;
| }
|
| public static Cloth GetFlagObj()
| {
| foreach (Thingy s in _Thingies)
| {
| if (s is Cloth)
| return (Cloth)s;
| }
| return null;
| }
|
| public static BedSprd GetFlagObj()
| {
| foreach (Thingy s in _Thingies)
| {
| if (s is BedSprd)
| return (BedSprd)s;
| }
| return null;
| }
|
|
| Is there a way I could have one generic method that would take as a
| parameter the item I wish returned?
Yes, you do it like this :
public static T GetObj<T>()
{
foreach (Thingy s in _Thingies)
{
if (s is T)
return (T) s;
}
return default(T);
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
可能的解决方案:使用参数创建通用搜索方法
必需类型,此方法的缺点 - 通用返回值
静态列表< object> _list = new List< object>();
公共静态对象查找(类型t)
{
返回_list。查找(委托(对象obj)
{
返回t.IsAssignableFrom(obj.GetType());
});
}
possible solution: create generic search method with argument of
required type, disadvantage of this method - generic return value
static List<object> _list = new List<object>();
public static object Find(Type t)
{
return _list.Find(delegate(object obj)
{
return t.IsAssignableFrom(obj.GetType());
});
}
作为对此的延伸;如果你有大量的物品
(每种不同的类型,如GetObj< T> [GetThingy< T>()
所建议的那样?) ])你可以通过使用字典<类型,
Thingy>来提高性能。添加时使用obj.GetType()作为键,当搜索
时使用typeof(T) - 这样就可以获得哈希表的性能。
另外 - 如果可以在静态ctor之外添加/删除东西,我会强烈建议使这个(静态)数据线程安全 - 例如
private static Dictionary< Type,Thingy> _Thingies; // init in static ctor
public static T GetThingy< T>()
{
lock(_Thingies){
返回(T)_Thingies [typeof(T)];
}
}
public static void Add(Thingy thingy){
if(thingy == null)抛出新的ArgumentNullException(" thingy"); //需要一个
非null的东西来调用GetType()
lock(_Thingies){
_Thingies.Add(thingy.GetType( ),好事);
}
}
//或者 - 特别是如果你接受thingy的空值
public static void添加< T>(T thingy)其中T:Thingy {
lock(_Thingies){
_Thingies.Add(typeof(T) ,东西);
}
}
(或类似的东西;未经测试的代码)
/>
Marc
As an extension to this; if you are going to have a large number of items
(each of a different type, as suggested by GetObj<T> [GetThingy<T>()
perhaps?]) you could improve performance by using a Dictionary<Type,
Thingy>; when adding you use obj.GetType() as the key, and when searching
you use typeof(T) - this then gives you hashtable performance.
Also - if thingies can be added / removed outside of the static ctor, I
would strongly advise making this (static) data thread-safe - e.g.
private static Dictionary<Type, Thingy> _Thingies; // init in static ctor
public static T GetThingy<T>()
{
lock(_Thingies) {
return (T) _Thingies[typeof(T)];
}
}
public static void Add(Thingy thingy) {
if(thingy==null) throw new ArgumentNullException("thingy"); // need a
non-null thingy to call GetType()
lock(_Thingies) {
_Thingies.Add(thingy.GetType(), thingy);
}
}
// or - particularly if you accept null values for thingy
public static void Add<T>(T thingy) where T : Thingy {
lock(_Thingies) {
_Thingies.Add(typeof(T), thingy);
}
}
(or something like that; code not tested)
Marc
这篇关于返回对象引用的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!