如何通过非泛型类函数传递泛型类型

时间:2020-03-04 17:42:23

标签: c# generics

我有一个将通用数据从API复制到通用数组(apiData.copyTo())的功能。我通过以下操作解决了这个问题:

public class Foo<T> {
    T[] data;

    public void SetData(Action<T[]> action) {
        action.Invoke(data);
    }
}

// ushort as example:
Foo<ushort> someFoo;
someFoo.SetData(buffer => apiData.copyTo(buffer));

但是,在对数据布局进行一些更改之后,我想为通用数据创建一个私有类,并使Foo类成为非通用类,这导致我面临以下问题:

public class Foo {

    private class Bar<T> {
        T[] data;

        public void SetData(Action<T[]> action) {
            action.Invoke(data);
        }
    }

    private Bar<ushort> bar; // Example generic, could be everything

    // The problem lies here:
    // T is not known to Foo, how to fix this?
    public void SetData(Action<T[]> action) {
        bar.SetData(action);

        // Some other stuff...
    }       
}

如您所见,问题是T类不知道泛型类型Foo。我如何仍将Action传递给Bar<T>内的私有类Foo

请注意,这是一个使我的问题更清楚的最小示例。

1 个答案:

答案 0 :(得分:0)

我认为这可以满足您的要求:

public class Foo
{
    public interface IBar
    {
        void SetData<T>(Action<T[]> action);
        Type GetBarType();
    }
    private class Bar<T>: IBar
    {
        //I changed this to a list since an empty array probably won't do you much good.
        List<T> data = new List<T>();

        public Type GetBarType()
        {
            return typeof(T);
        }

        public void SetData<TAction>(Action<TAction[]> action)
        {
            if(typeof(T) == typeof(TAction))
                action.Invoke(data as TAction[]);
        }
    }

    private IBar bar; 

    public void SetData<T>(Action<T[]> action)
    {
        if(bar?.GetBarType() == typeof(T))
            bar.SetData(action);
        else
        {
            bar = new Bar<T>();
            bar.SetData(action);
        }
        // Some other stuff...
    }
}

public class DoStuff
{
    public DoStuff()
    {
        var foo = new Foo();
        foo.SetData<int>((data) => {  /*do somthing wth this container*/ });
        foo.SetData<byte>((data) => {  /*do somthing wth this container*/ });
        foo.SetData<ushort>((data) => {  /*do somthing wth this container*/ });
        foo.SetData<ulong>((data) => {  /*do somthing wth this container*/ });
        foo.SetData<string>((data) => {  /*do somthing wth this container*/ });
    }
}