Roslyn: New features proposal for generics and other

Created on 21 Jan 2018  路  2Comments  路  Source: dotnet/roslyn

  1. Casting to a generic class/interface/struct without specifying generic types + ability to call its non-generic methods without using interfaces:
class Foo<T, U>
{
    public bool Test() => true;
    public T GenericFoo() => default(T);
    public void GenericFoo(T value) { }
}

object obj = new Foo<int>();
((Foo<,>)obj).Test(); //GenericFoo methods of class Foo<,> should not be available in this case.
Foo<,> foo = (Foo<,>)obj;
((Foo<int,byte>)foo).GenericFoo()
  1. Support of Delegate and Enum generic constraints:

class Foo<T> where T: Delegate

  1. Support of generics with generic parameter, at least one level:

class Foo<T<U>>
But I very much suspect that it's not possible or too hard to implement.

  1. Support of 'var' for field declarations:

public static var Foo = 0;

  1. Try/Catch/Finally without explicit block declaration:
{
    Foo();
    return true;
}
catch return false;

  1. Rethrow operator for saving the original stacktrace:
{

}
catch(Exception ex)
{
    rethrow ex; // bad example, but it's the same as just "throw;" in this case.
}

  1. memberof operator for accessing Type members metadata without using reflection:
class Foo
{
    public int Bar => this.bar;
    private int bar;
    public int this[string key] => 0;

    public MemberInfo[] GetMembers<T>(bool someValue)
    {
        MethodInfo mi = memberof(this.ToString); // no need to cast
        return new []
        { 
             memberof(this.Bar), 
             memberof(this.bar),
             memberof(this.GetMembers<T>(bool)), // specifying parameter types in case of overloading.
             memberof(string.GetEnumerator), // It works like nameof operator
             memberof(this[string]) // even indexers..
        };
    }
}

Also implementation must not use the search by function name using strings or something like, because it won't work after code obfuscation.

Area-Language Design

Most helpful comment

C# language change requests are taken at the http://github.com/dotnet/csharplang/ repository now. You'd want to make separate proposals rather than one mega-proposal. Many of these requests have already been made in one form or another, some championed and some already explained why they're not practical.

All 2 comments

C# language change requests are taken at the http://github.com/dotnet/csharplang/ repository now. You'd want to make separate proposals rather than one mega-proposal. Many of these requests have already been made in one form or another, some championed and some already explained why they're not practical.

@DenisKudelin I'll go ahead and close the issue and let you re-open on csharplang. Thanks

Was this page helpful?
0 / 5 - 0 ratings