c# 4.0 - Is there a way to new a class that's the same type as another class without directly specifying it -
i have extension method this
public static void dostuff(this objectcontext context) { using(var newcontext = new myentitiescontext()) { // stuff newcontext.savechanges(); } context.savechanges(); }
i wondering if there way new context of same type context passed in instead of specifying myentitiescontext?
thanks in advance
if don't mind reflection:
var context = activator.createinstance(context.gettype());
now either need base type or - if still don't mind reflection - can call method name. or, since using c#4 go dynamic.
edit: go way:
public static void dostuff<t>(this t context) t : objectcontext, new() { using(var newcontext = new t()) { // stuff newcontext.savechanges(); } context.savechanges(); }
Comments
Post a Comment