我正在将一个c#项目移植到silverlight windows phone 7.你知道Type类的TypeHandle.Value有什么替代方案吗?
我无法找到"价值"在TypeHandle定义中。
以下是我的代码中的示例:
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;
}
提前感谢。
答案 0 :(得分:2)
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;
}