【问题标题】:Implement extension Method C#实现扩展方法 C#
【发布时间】:2015-02-15 18:01:51
【问题描述】:

我需要实现这个方法:Reflectable reflect<T>(IEnumerable<T> src) 但我很难获得预期的输出。 希望有人能帮助我,谢谢。

这里是Reflectable接口:

interface Reflectable : IEnumerable<string> { Reflectable Prop(string propName);}

和预期的输出:

IEnumerable<Student> stds = //Students
IEnumerable<String> r1 = reflect(stds).Prop("Id"); 
IEnumerable<String> r2 = reflect(stds).Prop("Name").Prop("Id").Prop("Age"); 
IEnumerable<String> r3 = reflect(stds);
r1.ToList().ForEach(Console.Write); // {3213}{3124}{35454}... 
r2.ToList().ForEach(Console.Write); // {Jose, 3213, 89}{Maria, 3124, 34}{Prominencia, 35454, 23}... 
r3.ToList().ForEach(Console.Write); // {}{}{}...

【问题讨论】:

  • 我需要实现反射方法来获取评论中的输出,但我不知道如何。
  • 您认为“扩展方法”是什么?因为我在这里找不到。
  • 我认为 Prop() 是 Reflect 的扩展方法
  • IEnumerable&lt;String&gt; r2 = reflect(stds).Prop("Name").Prop("Id").Prop("Age"); 不可能。如果reflect 返回IEnumerable&lt;String&gt;,则无法连接。
  • 您的问题似乎是关于反射/反射的。 Prop() 可能是一个扩展方法,但不清楚是什么类型。

标签: c# reflection extension-methods


【解决方案1】:

我仍然认为你的这个想法应该在它的睡眠中被谋杀,或者在出生之前......

public class ReflectionHelper<T>
    : IEnumerable<string>
{
    private string[] _properties;
    private IEnumerable<T> _list;

    public ReflectionHelper(IEnumerable<T> list, string[] properties)
    {
        _properties = properties;
        _list = list;
    }

    public ReflectionHelper<T> Prop(string property)
    {
        return new ReflectionHelper<T>(_list, _properties.Concat(new string[]{ property}).ToArray());
    }

    public ReflectionHelper<T> Prop(string property)
    {
        return new ReflectionHelper<T>(_list, _properties.Concat(new string[] { property }).ToArray());
    }

    public static implicit operator List<string>(ReflectionHelper<T> helper)
    {
        return helper._list.Select(item => string.Join(",",
                        (from p in helper._properties
                         select typeof(T).GetProperty(p).GetValue(item, null)).ToArray())).ToList();
    }

    public IEnumerator<string> GetEnumerator()
    {
        return _list.Select(item => string.Join(",",
                                                (from p in _properties
                                                 select typeof (T).GetProperty(p).GetValue(item, null)).ToArray()))
                    .GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public static class ReflectionHelperExtension
{
    public static ReflectionHelper<T> Prop<T>(this IEnumerable<T> items, string property)
    {
        return new ReflectionHelper<T>(items, new string[] { property });
    }
}

【讨论】:

  • 我在您的代码中收到了 VS 中的一些编译时错误。你测试了吗?
【解决方案2】:

如果我了解您要实现的目标,扩展方法将是一种方法,方法如下:

// This class has to be static for extension methods to be detected
public static class MyExtensions
{
    // using "this" before the first parameter will make it an extension of the type
    public static IEnumerable<string> Prop<T>(this IEnumerable<T> enumerable, string propName)
    {
        var type = typeof(T);
        // Note that this can throw an exception if the property is not found
        var info = type.GetProperty(propName);

        // Here are your students, considering that <T> is <Student>
        foreach (var item in enumerable) 
        {
            // return the value fromt the item, I'm using ToString here for
            // simplicity, but since you don't know the property type in
            // advance, you can't really do anything else than assumne its
            // type is plain "object"
            yield return info.GetValue(item).ToString();
        }
    }
}

你不能做的一件事是像@lomed 所说的那样链接它们,因为它返回一个 IEnumerable。

【讨论】:

    【解决方案3】:
    public static class EnumerableReflect
    {
        private static string OneElementReflect<T>(T e, PropertyInfo[] props)
        {
            var values = props.Select(x => x.GetValue(e).ToString());
            return "{" + string.Join(", ", values)) + "}";
        }
    
        public static IEnumerable<string> enumerbleString<T>(this IEnumerable<T> collection, params string[] PropertysNames)
        {
            var props = PropertysNames.Select(x => typeof(T).GetProperty(x)).ToArray();
            return collection.Select(x => OneElementReflect(x, props));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多