开发者

Using different generic types on a method's argument and return type

开发者 https://www.devze.com 2023-03-11 20:08 出处:网络
I am working on a generic utility method that takes a generic argument and returns a generic type--I hope that makes sense!--but I want the return type to be a different type from the argument.

I am working on a generic utility method that takes a generic argument and returns a generic type--I hope that makes sense!--but I want the return type to be a different type from the argument.

Here's what I'm thinking this should look like if I mock it up in pseudo code:

public static IEnumerable<R> DoSomethingAwesome<T>(T thing) 
{
    var results = new List<R>();

    for (int xx = 0; xx < 5; xx++)
    {
        results.Add(thing.ToRType(xx));
    }

    return results;
}

With generics not being able to开发者_开发知识库 infer the return type how would I go about doing something like this? So far, my Google-Fu has failed me.


// You need this to constrain T in your method and call ToRType()
public interface IConvertableToTReturn
{
    object ToRType(int someInt);
}

public static IEnumerable<TReturn> DoSomethingAwesome<T, TReturn>(T thing)
    where T : IConvertableToTReturn
{
    Enumerable.Range(0, 5).Select(xx => thing.ToRType(xx));
}


You can pass the return class as an output parameter:

public static void DoSomethingAwesome<T,R>(T thing, out IEnumerable<R> output) 

This can then be inferred.


static IEnumerable<R> Function<T,R> (T h)
{
    for (int xx = 0; xx < 5; xx++)
    {
        yield return h.ToRType(xx);
    }
    yield return break;
}

IEnumerable<class2> res =  Function<class1, class2>(class1Object);


You need to explicitly specify the return generic type as a type parameter to the method.

Something like:

public static IEnumerable<R> DoSomething<T,R>(IEnumerable<T> things, Func<T,R> map) 
{
    foreach (var t in things) { yield return map(t); }
}

This is essentially what the Linq IEnumerable extension method "Select" does..


Generics can be awesome and a pretty awesome pain. As other have stated you can use a variety of ways to have multiple in put parameters the real trick is in doing something usefully with the passed in types.

in Your example

public static IEnumerable<Ret> Fn<Ret,Parm>(IList<Parm> P)  
        {
            var Results = new List<Ret>();
            foreach(Parm p in P)
            {
                Results.Add(p.ToType());
            }
            return Results;
        }

Will not complie since the complier doesn't know what to do with P.ToType()

So you say well I can just add the function needed to my param type But that doesn't work either since the complier again doesn't know what the concrete version or Ret will be and your return list is of type Ret not of type returnType

public class RetunType
        {
            public int a;
        }

        public class Input
        {
            public int x;
            public RetunType TotoAReturnType()
            {
                return new RetunType() { a = this.x };
            }
        }

        public static IEnumerable<Ret> Fn<Ret, Parm>(IList<Parm> P) where Parm : Input  where Ret:RetunType
        {
            var Results = new List<Ret>();
            foreach (Parm p in P)
            {
                Results.Add(p.TotoAReturnType());
            }
            return Results;
        }

To solve this issue you can add a generic interface so that your function can work if any type supports the generic interface

Like this

public interface ToType<R>
{
    R ToType();
}
public class B
{
    public int x; 
}

public class A : ToType<B>
{
    string x = "5";
    public B ToType()
    {
        B aB = new B();
        aB.x = int.Parse(x);
        return aB;
    }
}
public static IEnumerable<Ret> Fn<Ret,Parm>(IList<Parm> P)  where Parm : ToType<Ret>
{
    var Results = new List<Ret>();
    foreach(Parm p in P)
    {
        Results.Add(p.ToType());
    }
    return Results;
}
static void Main(string[] args)
{
    List<A> inLst = new List<A>() { new A()};
    var lst = Fn<B, A>(inLst);
}

Generics are awesome but I would strongly suggest looking to using interfaces to support you actions in those functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消