实现扩展方法C#

时间:2015-02-15 18:01:51

标签: c# reflection extension-methods

我需要实现此方法:Reflectable reflect<T>(IEnumerable<T> src) 但我有麻烦来获得预期的产量。 希望有人能帮助我,谢谢。

这里的界面反映:

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); // {}{}{}...

3 个答案:

答案 0 :(得分:1)

如果我了解您所实现的目标,那么扩展方法就是一种方法,以下是:

// 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。

答案 1 :(得分: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 });
    }
}

答案 2 :(得分:0)

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