我正在将一个 C# 项目移植到 silverlight Windows Phone 7。您知道 Type 类的 TypeHandle.Value 的替代方案是什么吗?
我在 TypeHandle 定义中找不到“Value”。
这是我的代码的示例:
public override IEnumerable<MemberInfo> GetSerializableMembers(Type type)
{
MemberInfo[] properties;
if (!memberCache.TryGetValue(type.TypeHandle.Value, out properties))
memberCache.Add(type.TypeHandle.Value, properties = base.GetSerializableMembers(type).ToArray());
return properties;
}
它无法在 WP7 中编译:
type.TypeHandle.Value
此代码中的 attributeType.TypeHandle.Value 也存在同样的问题:
T GetSingleAttributeOrDefault<T>(PropertyInfo propertyInfo) where T : Attribute, new()
{
Type attributeType = typeof(T);
Attribute attribute;
var key = new PointerPair(propertyInfo.GetGetMethod().MethodHandle.Value, attributeType.TypeHandle.Value);
if (!attributeCache.TryGetValue(key, out attribute))
attributeCache.Add(key, attribute = base.GetSingleAttributeOrDefault<T>(propertyInfo));
return attribute as T;
}
提前致谢。
最佳答案
谢谢乔恩。 我改变了 key 。现在它编译了。 这是新代码:
readonly Dictionary<Tuple<Type, MethodInfo>, Attribute> attributeCache = new Dictionary<Tuple<Type, MethodInfo>, Attribute>();
T GetSingleAttributeOrDefault<T>(PropertyInfo propertyInfo) where T : Attribute, new()
{
Type attributeType = typeof(T);
Attribute attribute;
//var key = new PointerPair(propertyInfo.GetGetMethod().MethodHandle.Value, attributeType.TypeHandle.Value);
var key = new Tuple<Type, MethodInfo>(attributeType, propertyInfo.GetGetMethod());
if (!attributeCache.TryGetValue(key, out attribute))
attributeCache.Add(key, attribute = base.GetSingleAttributeOrDefault<T>(propertyInfo));
return attribute as T;
}
关于c# - typeof(object).TypeHandle.Value 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765957/