Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brendanjerwin/81609 to your computer and use it in GitHub Desktop.
Save brendanjerwin/81609 to your computer and use it in GitHub Desktop.
using System;
namespace TestAnonymousGeneric
{
internal static class MyClass
{
public static MyClass<ET> Build<ET>(ET valValue)
{
return new MyClass<ET> { val = valValue };
}
}
internal class MyClass<T>
{
public T val { get; set; }
}
internal class MoreComplex
{
public string foo { get; set; }
}
class Program
{
static void Main(string[] args)
{
//This one doesn't need a generic type specification to get to the static factory method.
var foo = MyClass.Build(new { Name = "This is a Name", Count = 1, Com = new MoreComplex() });
//foo.val.Name = "Can't write to this. It's read only.";
Console.WriteLine(foo.val.Name);
foo.val.Com.foo = "This is writable";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment