Error at:
public partial class Form2 : Form
Probable cause:
public static IChromosome To<T>(this string text) { return (IChromosome)Convert.ChangeType(text, typeof(T)); }
Attempted (without static keyword):
public IChromosome To<T>(this string text) { return (IChromosome)Convert.ChangeType(text, typeof(T)); }
Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter is preceded by the this modifier.
Actually I'm answering the question of why extension methods cannot work with static classes. The answer is because extension methods are compiled into static methods that cannot recieve a reference to a static class.
No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.
Here, this keyword is used for binding, Geek is the class name in which you want to bind, and g is the parameter name. Extension methods are always defined as a static method, but when they are bound with any class or structure they will convert into non-static methods.
If you remove "this" from your parameters it should work.
public static IChromosome To<T>(this string text)
should be:
public static IChromosome To<T>(string text)
The class containing the extension must be static. Yours are in:
public partial class Form2 : Form
which is not a static class.
You need to create a class like so:
static class ExtensionHelpers { public static IChromosome To<T>(this string text) { return (IChromosome)Convert.ChangeType(text, typeof(T)); } }
To contain the extension methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With