实现扩展方法 C#

Implement extension Method C#

我需要实现这个方法: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); // {}{}{}...

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

// 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 所说的那样将它们链接起来,因为它 returns 一个 IEnumerable。

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));
    }
}

我仍然认为你的这个想法应该在睡梦中或出生前被扼杀...

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 });
    }
}