c# - typeof(object).TypeHandle.Value 的替代方案

标签 c# silverlight windows-phone-7

我正在将一个 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/

相关文章:

c# - 如何 'await' WebClient.UploadStringAsync 请求?

c# - 如何在开发时将现有文件添加到 IsolatedStorage?

c# - AWS Lambda 访问文件

c# - 在 WP7 的 silverlight 中删除图像源

c# - 单元测试 COM 事件?

wpf - WPF/Silverlight 的 TDD 类型方法

c# - Scripting.FileSystemObject Write 方法失败

c# - ScrollViewer 和 LonglistSelector Scrollbar 冲突

c# - Office 365 任务 API - 在 web.config C# 中使用固定用户凭据访问 api

c# - 为具有不同事件处理程序委托(delegate)的多个控件实现单个事件处理程序