internal static T Create <T>(string args) where T : IMyType { IMyType newThing = (T)typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, Type.EmptyTypes, null).Invoke(null); newThing.Initialise(args); return((T)newThing); }
internal static T Create <T>(string args) where T : IMyType { IMyType newThing = (T)typeof(T).GetMethod("GetInstance").Invoke(default(object), null); newThing.Initialise(args); return((T)newThing); }
static void Main(string[] args) { var item = new MyType(); // Option 1 - Implicit cast. Compile time checked but takes two lines. IMyType item2 = item; System.Console.WriteLine(item2.SayHello()); // Option 2 - One line but risks an InvalidCastException at runtime if MyType changes. System.Console.WriteLine(((IMyType)item).SayHello()); // Option 3 - One line but risks a NullReferenceException at runtime if MyType changes. System.Console.WriteLine((item as IMyType).SayHello()); // Option 4 - compile time one liner Converter.ReturnAs <IMyType>(item).SayHello(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime, IMyType myType) { myType.SayHello(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
public ValuesController(IMyType mytype) { _mytype = mytype; }
public MyController(IMyType theType) { this.theType = theType; }
public static void MyLogic(this IMyType myType) { // whatever other logic is needed myType.Notes = "notes"; myType.Price = 1; }
public abstract void Method1(IMyType parameter);
public static void SetMyType(IMyType _type) { __type = _type; }