【问题标题】:Reflection of comparison phrase比较短语的反映
【发布时间】:2012-10-30 13:58:54
【问题描述】:

也许我的问题很奇怪......我想知道是否可以对一个短语使用反射。

我试图与 C# 中的反射进行比较。到目前为止,我将属性的名称作为字符串传递,将值作为对象传递,如下所示:Cmp("foo", "abc")
这样我必须检查foo是否是类中的现有属性,并检查值类型是否与属性类型匹配(在上面的示例中 foo 是字符串属性,值是字符串)。这种方法很好用!

我只是想知道是否可以将短语作为参数发送并用反射或类似的东西对其进行分析。
我的意思是,就像上面的例子一样,而不是像Cmp("foo", "abc")那样调用函数,只需像这样调用Cmp(A.foo == "abc")这样的函数(A是具有foo属性的类),然后分析该属性是foo并且值为"abc"

我知道这听起来很奇怪,而且对我来说没有必要。它只是为了这个想法。
有可能吗?

编辑
如果我不清楚,我已经写了Cmp(string, string)方法,它工作正常!
我只想知道是否有办法像这样编写Cmp 方法:Cmp(A.foo == "abc")。该参数是一个短语。

编辑 2
例如,您可以在 C 中执行类似的操作。您可以这样创建宏:

#define Cmp(phrase) printf(##phrase)

然后,如果您将其称为 Cmp(A.foo == "abc"),则输出将是:

A.foo == "abc"

就像将整个短语作为参数传递并对其进行分析。我知道宏是预编译的东西,我只想知道C#中是否有类似的东西

【问题讨论】:

  • 我不明白这个问题。如果obj.Foo == "abc"可以,Cmp可以做什么?
  • 对不起,我会解决这个问题,包含Cmp 的类也包含具有foo 属性的对象。应该是A.foo == "abc"
  • 您可能需要发布更完整的代码才有意义。
  • 基本上你想遍历你调用Cmp的对象的所有属性,并检查它是否有一个名为Foo的属性并比较值?

标签: c# reflection parameters comparison phrase


【解决方案1】:

您可以使用expression trees 来描述像bar.Foo == "abc" 这样的表达式。这是一个简单的示例,假设您有一个名为 Bar 的类,该类有一个名为 Foo 的属性:

String FormatExpression<T>(Expression<Func<T, Boolean>> expression) {
  if (expression == null)
    throw new ArgumentNullException("expression");
  var body = expression.Body as BinaryExpression;
  if (body == null)
    throw new ArgumentException(
      "Expression body is not a binary expression.", "expression");
  return body.ToString();
}

打电话

FormatExpression((Bar bar) => bar.Foo == "abc")

将返回字符串

(bar.Foo == "abc")

【讨论】:

  • 我已经写了Cmp(string, string);。我只是想知道是否可以像Cmp(A.foo == "abc"); 这样写。
  • @Someone:我不明白你编辑的问题。你肯定不是在问函数Boolean Cmp(Boolean b) { return b; }(它满足你的要求),所以一定有一些我遗漏的细节。
  • 我的问题已经结束了。如果 yue 可以发送孔短语作为参数。我将使用 C 中的示例再次编辑
【解决方案2】:

此扩展方法会遍历它所调用的对象的所有(可读、公共)属性,检查该属性上具有给定名称的属性并比较值。

public static class CmpExtension
{
    public static bool Cmp<T, TValue>(this T obj, string propertyName, TValue value)
        where TValue : class
    {
        var properties = obj.GetType().GetProperties()
                .Where(p => p.CanRead);

        foreach (var property in properties)
        {
            var propertyValue = property.GetValue(obj, null);

            var childProperty = property.PropertyType.GetProperties()
                .Where(p => p.CanRead)
                .FirstOrDefault(p => p.Name == propertyName);

            if (childProperty == null) continue;

            var childPropertyValue = childProperty.GetValue(propertyValue, null);

            return childPropertyValue == value;
        }

        return false;
    }
}

通过使用这个,你可以做到:

public class Foo
{
    public Bar Bar { get; set; }
}

public class Bar
{
    public string Value { get; set; }
}

public static void Main(string[] args)
{
    var foo = new Foo { Bar = new Bar { Value = "Testing" } };
    foo.Cmp("Value", "Testing"); // True
}

要使用替代语法,请使用:

public static class CmpExtension
{
    public static bool Cmp<T>(this T obj, Func<T, bool> func)
    {
        return func(obj);
    }
}

使用这个,你可以做到

    public static void Main(string[] args)
    {
        var foo = new Foo { Bar = new Bar { Value = "Testing" } };
        foo.Cmp(f => f.Bar.Value == "Testing"); // True
    }

【讨论】:

  • 我已经写了Cmp(string, string);。我只是想知道是否可以像Cmp(A.foo == "abc"); 这样写。
猜你喜欢
  • 2011-09-25
  • 2013-11-19
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多