C# 实现扩展方法C

C# 实现扩展方法C,c#,reflection,extension-methods,C#,Reflection,Extension Methods,我需要实现这个方法:Reflectable Reflectable src 但是我很难得到预期的产量。 希望有人能帮助我,谢谢 这里的界面是可反射的: 接口可反射:IEnumerable{Reflectable Propstring propName;} 以及预期产量: IEnumerable<Student> stds = //Students IEnumerable<String> r1 = reflect(stds).Prop("Id"); IEnumerable

我需要实现这个方法:Reflectable Reflectable src 但是我很难得到预期的产量。 希望有人能帮助我,谢谢

这里的界面是可反射的:

接口可反射:IEnumerable{Reflectable Propstring 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所说的那样将它们链接起来,因为它返回一个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 });
    }
}

我需要实现reflect方法来获取注释中的输出,但我不知道如何实现。您认为“扩展方法”是什么?因为我在这里找不到一个。我认为Prop是ReflectionEnumerable r2=reflectstds.PropName.PropId.PropAge的扩展方法;不可能的如果reflect return IEnumerable,则无法连接它。您的问题似乎与reflect/reflect有关。Prop可能是一个扩展方法,但不清楚是什么类型。我在你的代码的VS中遇到了一些编译时错误。你在测试它吗?