如何将对象转换为通用字典? [英] How to cast objects to generic dictionary?
问题描述
通用字典如下:
public class ConcurrentDictionary< TKey,TValue> :IDictionary< TKey,TValue>
具体的字典可以如下:
var container = new ConcurrentDictionary< string,Unit>();
var container = new ConcurrentDictionary< string,CustomUnitClass>();
应用程序状态中已添加这些特殊字典(具有不同参数):
HttpContext.Current.Application [key] = container;
当我从应用程序状态中获取项目(有些人在这里帮助我,谢谢他们)我可以通过这种方式检查类型是否是ConcurrentDictionary:
object d = HttpContext.Current.Application [i] ;
if(d.GetType()。GetGenericTypeDefinition()== typeof(ConcurrentDictionary<,>))
最后一点是 - 如何将对象d转换为通用ConcurrentDictionary:
ConcurrentDictionary<?,? > unit = d作为ConcurrentDictionary<?,?> ;;
我不想使用特定的演员如下:
ConcurrentDictionary< string,Unit> unit = d作为ConcurrentDictionary< string,Unit> ;;;
因为第二个参数可以是其他类型。
谢谢你提前。
我认为你可能正在看泛型一个稍微错误的方式,但没关系,让我们谈谈!
您的 IDictionary< TKey,TValue>
而且您正在正确使用它,但是我认为如果要回溯,除非您明确知道您期望的是什么类型,否则在投射时不会有 。
或者,我建议,为了强烈的可爱的目的;你提到可能的第二种类型,也就是 TValue
将会有所不同...这是使用界面的完美时间!让我演示。
我们的界面
public interface IModeOfTransport
{
string Name {get;组; }
string颜色{get;组; }
bool CanFloat {get;组; }
bool CanFly {get;组; }
int NumberOfPassengers {get;组;
}
我们的对象
public class Car:IModeOfTransport
{
// ...
}
public class Plane:IModeOfTransport
{
// ...
}
public class Boat:IModeOfTransport
{
// ...
}
我们的实现
var modesOfTransport = new字典< string,IModeOfTransport>();
modesOfTransport.Add(First,new Car());
modesOfTransport.Add(First,new Plane());
modesOfTransport.Add(First,new Boat());
让我们休息一下
可以从上面,我们有一个关键类型为 string
和值类型为$ code> IModeOfTransport 的字典。这允许我们显式强力类型地访问 IModeOfTransport
接口的所有属性和方法。如果您需要访问字典中的通用值的特定信息,并且您不知道实际的对象类型转换是什么,则建议您使用。通过使用界面,我们可以找到相似之处。
几乎完成了
对象字典= HttpContext.Current.Application [i];
if(dictionary.GetType()。GetGenericTypeDefinition()== typeof(Dictionary<,>))
{
var modesOfTransport = dictionary as Dictionary< string,IModeOfTransport> ;;
foreach(var keyValuePair in modesOfTransport)
{
// ...
}
}
The generic dictionary is as follows:
public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>
And specific dictionaries can be as follows:
var container = new ConcurrentDictionary<string, Unit>();
var container = new ConcurrentDictionary<string, CustomUnitClass>();
These special dictionaries (with different parameters) have been added in Application state:
HttpContext.Current.Application[key] = container;
When I get the items from Application state (some people here helped me about that; Thanks them), I'm able to check if type is of ConcurrentDictionary in this way:
object d = HttpContext.Current.Application[i];
if (d.GetType().GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>))
And the last point was left - how to cast object d to generic ConcurrentDictionary:
ConcurrentDictionary<?, ?> unit = d as ConcurrentDictionary<?, ?>;
I don't want to use specific cast as follows:
ConcurrentDictionary<string, Unit> unit = d as ConcurrentDictionary<string, Unit>;
because the second parameter can be of another type.
Thank you in advance.
I think you might be looking at generics in a slightly wrong way, but that's okay, lets talk!
Your IDictionary<TKey, TValue>
is perfect, and you're using it correctly, however I think when it comes to casting back, unless you explicitly know what the type is you're expecting, there's no real point in casting it.
Alternatively what I would recommend, for the purpose of strongly-typed loveliness; you mentioned that potentially the second type, aka the TValue
will vary... this is a perfect time to use an interface! Let me demonstrate.
Our interface
public interface IModeOfTransport
{
string Name { get; set; }
string Colour { get; set; }
bool CanFloat { get; set; }
bool CanFly { get; set; }
int NumberOfPassengers { get; set; }
}
Our objects
public class Car : IModeOfTransport
{
// ...
}
public class Plane : IModeOfTransport
{
// ...
}
public class Boat : IModeOfTransport
{
// ...
}
Our implementation
var modesOfTransport = new Dictionary<string, IModeOfTransport>();
modesOfTransport.Add("First", new Car());
modesOfTransport.Add("First", new Plane());
modesOfTransport.Add("First", new Boat());
Let's take a break
As you can from the above, we have a dictionary with a key type of string
and a value type of IModeOfTransport
. This allows us explicit strongly-typed access to all of the properties and methods of the IModeOfTransport
interface. This is recommended if you need to access specific information on a generic value within a dictionary, and you don't know what the actual object type cast is. By using interfaces it allows us to find similarities.
Almost done
object dictionary = HttpContext.Current.Application[i];
if (dictionary.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
var modesOfTransport = dictionary as Dictionary<string, IModeOfTransport>;
foreach (var keyValuePair in modesOfTransport)
{
// ...
}
}
这篇关于如何将对象转换为通用字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!