使用动态关键字访问常量

发布于 2024-11-02 15:13:56 字数 388 浏览 0 评论 0原文

这是如何调用的后续内容C#4.0 中具有动态类型的静态方法?

在使用 double.MaxValue、int.MaxValue 等使用动态关键字和/或泛型时,有没有办法消除重复?

人为的例子:

  T? Transform<T>(Func<T?> continuation)
     where T : struct
  {
     return typeof(T).StaticMembers().MaxValue;
  }

This is a follow-up to How to invoke static method in C#4.0 with dynamic type?

Is there a way to remove duplication when working with double.MaxValue, int.MaxValue, etc. by using dynamic keyword and/or generics?

Contrived example:

  T? Transform<T>(Func<T?> continuation)
     where T : struct
  {
     return typeof(T).StaticMembers().MaxValue;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-11-09 15:13:56

以这种方式修改类StaticMembersDynamicWrapper

  public override bool TryGetMember(GetMemberBinder binder, out object result) { 
    PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); 
    if (prop == null) { 
        FieldInfo field = _type.GetField(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); 
        if (field == null)
        {
            result = null;
            return false; 
        }
        else
        {
            result = field.GetValue(null, null);
            return true; 
        }
    } 

    result = prop.GetValue(null, null); 
    return true; 
}

代码的问题是它只检索属性,但常量实际上是字段。

Modify class StaticMembersDynamicWrapper in this way:

  public override bool TryGetMember(GetMemberBinder binder, out object result) { 
    PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); 
    if (prop == null) { 
        FieldInfo field = _type.GetField(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public); 
        if (field == null)
        {
            result = null;
            return false; 
        }
        else
        {
            result = field.GetValue(null, null);
            return true; 
        }
    } 

    result = prop.GetValue(null, null); 
    return true; 
}

Problem of your code is that it only retrieves properties, but constants are actually fields.

玩世 2024-11-09 15:13:56

具有一点风格和天赋,相同的代码:

  public override bool TryGetMember(GetMemberBinder binder, out object result)
  {
     PropertyInfo prop = _type.GetProperty(binder.Name,
        BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);

     if (prop != null)
     {
        result = prop.GetValue(null, null);
        return true;
     }

     FieldInfo field = _type.GetField(binder.Name,
        BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);

     if (field != null)
     {
        result = field.GetValue(null);
        return true;
     }

     result = null;
     return false;
  }

和一个缓存类,以避免创建不需要的对象:

public static class StaticMembersDynamicWrapperExtensions
{
   static Dictionary<Type, DynamicObject> cache = 
      new Dictionary<Type, DynamicObject>
      {
         {typeof(double), new StaticMembersDynamicWrapper(typeof(double))},
         {typeof(float), new StaticMembersDynamicWrapper(typeof(float))},
         {typeof(uint), new StaticMembersDynamicWrapper(typeof(uint))},
         {typeof(int), new StaticMembersDynamicWrapper(typeof(int))},
         {typeof(sbyte), new StaticMembersDynamicWrapper(typeof(sbyte))}
      };

   /// <summary>
   /// Allows access to static fields, properties, and methods, resolved at run-time.
   /// </summary>
   public static dynamic StaticMembers(this Type type)
   {
      DynamicObject retVal;
      if (!cache.TryGetValue(type, out retVal))
         return new StaticMembersDynamicWrapper(type);

      return retVal;
   }
}

With a little style and flair, same code:

  public override bool TryGetMember(GetMemberBinder binder, out object result)
  {
     PropertyInfo prop = _type.GetProperty(binder.Name,
        BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);

     if (prop != null)
     {
        result = prop.GetValue(null, null);
        return true;
     }

     FieldInfo field = _type.GetField(binder.Name,
        BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);

     if (field != null)
     {
        result = field.GetValue(null);
        return true;
     }

     result = null;
     return false;
  }

And a cache class, to avoid creating unneeded objects:

public static class StaticMembersDynamicWrapperExtensions
{
   static Dictionary<Type, DynamicObject> cache = 
      new Dictionary<Type, DynamicObject>
      {
         {typeof(double), new StaticMembersDynamicWrapper(typeof(double))},
         {typeof(float), new StaticMembersDynamicWrapper(typeof(float))},
         {typeof(uint), new StaticMembersDynamicWrapper(typeof(uint))},
         {typeof(int), new StaticMembersDynamicWrapper(typeof(int))},
         {typeof(sbyte), new StaticMembersDynamicWrapper(typeof(sbyte))}
      };

   /// <summary>
   /// Allows access to static fields, properties, and methods, resolved at run-time.
   /// </summary>
   public static dynamic StaticMembers(this Type type)
   {
      DynamicObject retVal;
      if (!cache.TryGetValue(type, out retVal))
         return new StaticMembersDynamicWrapper(type);

      return retVal;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文