Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Generic Types as Method<T...> #1372

Closed
sgf opened this issue Mar 11, 2018 · 8 comments
Closed

Dynamic Generic Types as Method<T...> #1372

sgf opened this issue Mar 11, 2018 · 8 comments

Comments

@sgf
Copy link

sgf commented Mar 11, 2018

how about add this new Feature(syntax) like:

public List<T..> ExeSql<T..>(string sql,params object[] objs)
{
var len=length(T..);
typeof(T..[0])
typeof(T..[1])
typeof(T..[2])
...
..
..
//or maybe use foreach
foreach(typeof(T) t in T..)
{
      t....
}


}

to instead of

public List<T1> ExeSql <T1> (string sql,params object[] objs)
public List<T1,T2> ExeSql<T1,T2>(string sql,params object[] objs)
public List<T1,T2,T3> ExeSql<T1,T2,T3>(string sql,params object[] objs)
<T1,T2,T3,T4>...
<T1,T2,T3,T4,T5>...
T6...
...
...
..
@svick
Copy link
Contributor

svick commented Mar 11, 2018

This looks like a duplicate of dotnet/roslyn#5058, which was closed with this message:

I am not moving this particular issue [from dotnet/roslyn to dotnet/csharplang] because I don't have confidence that the LDM would likely consider doing this as proposed. I think we might consider doing something along these lines along with higher-kinded types if we do that; see #339.

@bondsbw
Copy link

bondsbw commented Mar 11, 2018

What is List<T1,T2> and List<T1,T2,T3>?

Where would this be useful?

@svick
Copy link
Contributor

svick commented Mar 11, 2018

@bondsbw I think it's effectively List<(T1, T2, T3)>. So, if you want to return a table with a person's name and age, you would have something like ExeSql<string, int>("select name, age from people").

@bondsbw
Copy link

bondsbw commented Mar 12, 2018

var items = ExeSql<(string, int)>("select name, age from people");
(string name, int age) = items[0];

Since that is legal today with a single type parameter, I'm not seeing anything very useful in this proposal.

@sgf
Copy link
Author

sgf commented Mar 12, 2018

@bondsbw
where if u want to design a API to return multi Result Types
such call like:

 Tuple<List<Account>,List<User>,List<UserInfo>,List<UserSetting>> ExeSql<Account,User,UserInfo,UserSetting>(.....);

 Tuple<List<Account>,List<User>,List<UserInfo>,List<UserSetting>.....{or more types result}>ExeSql<Account,User,UserInfo,UserSetting,.....{or more types result}>(.....);

u should be define lots of overload Methods.

but if we can just define once time.thats will be Write less code,yes,we just need write once in One Method.

@sgf sgf changed the title Dymic Generic Types as Method<T...> Dynamic Generic Types as Method<T...> Mar 12, 2018
@sgf
Copy link
Author

sgf commented Mar 12, 2018

@bondsbw
sorry my project is very old
let me try whats ur submit(use new Tuple syntax ).
if i can define like thats,i will close this issues

@sgf
Copy link
Author

sgf commented Mar 12, 2018

@bondsbw
these codes could be Compiled


        public class TestClass {

        }

        public List<T> TestMethod<T>(){


            return new List<T>();
        } 

            Test<(string, int, TestClass)>();

but how can i get the Types from T with argument of Tuple in TestMethod ?

because I needed to construct the types objects to return;

@sgf
Copy link
Author

sgf commented Mar 12, 2018

i found it GetGenericArguments. @bondsbw thank u,and thanks to all.

public  class Program
    {
        public class TestClass
        {
        }
        public static List<T> Test<T>()
        {
            var types = typeof(T).GetGenericArguments().ToList();
            types.ForEach(t => Console.WriteLine(t.Name));
            return new List<T>();
        }
        static void Main(string[] args)
        {
            Test<(string, int, TestClass)>();
            Console.ReadKey();
        }
    }

@sgf sgf closed this as completed Mar 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants