c# - Shorthand for instantiation and initialization using an extension method -


Is there any cloak to use the extension method to instantiate and start an object?

My goal is

Example:

  // ... Completely code the code needed to cover the appropriate code for the client It is necessary to start and get started. Code - I want to avoid the construction of tap example MyType t = null; T = t. Gate test instances (); // ... // extension method public stable class MyTypeExtensions {public static MyType GetTestInstance (this MyTypeT) {var ctorInjectedDependency = blah; Return the new MyType (ctorInjectedDependency); }}  

Perhaps something similar will be to your needs:

  Public Class MyType {Public MyType (string S) {property = s; } Public String Property {Received; Set; }} Public stable class MyTypeExtensions {Public static object GetTestInstance (thus type) {var ctorInjectedDependency = "blah"; Var ctorInfo = t.GetConstructor (new [] {typef (string)}); Return ctorInfo.Invoke (new object [] {ctorInjectedDependency}); } GetStateInstance Public Relatable & lt; T & gt; (Thus T) {var ctorInjectedDependency = "blah"; Var ctorInfo = t.GetConstructor (new [] {typef (string)}); Return (t) ctorInfo.Invoke (new object [] {ctorInjectedDependency}); }}  

Usage:

  var my = typeof (MyType) .GetTestInstance (); // Maybe the object is enough my2 = typeof (MyType) .GetTestInstance & lt; MyType & gt; (); Console.light line (like my mytype). Property); Console.WriteLine (my2.Property);  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -