I am trying to understand an extension method similar to this code
var p = new Person("Tim"); p.LastName = "Meier"; // reader.Get<bool>("IsDerivat"); var IsOlivia = p.Get<bool>("Olivia");
This is my code inside RoslynPad :
public static class PersonExtensions { public static T Get<T>(this Person person, string name) { return (T)person.NewFirstName(name); } } public class Person { public Person(string firstName) { this.FirstName = firstName; } public string FirstName {get; private set;} public string LastName {get; set;} public object NewFirstName(string name) { this.FirstName = name; return (object) this.FirstName; } }
But I get this error
bug CS1109: extension methods must be defined in the static static level class; PersonExtensions is a nested class
I found this question extension-methods-must-be-defined-in-a-top-level-static-class- and the answers are good.
Adding namespace Foo returns
error CS7021: cannot declare namespace in script code
Roslynpad seems to add things backstage. So, how can I make sure my extension method is defined in a top-level static class?
source share