c# - Override the default behaviour of GetEnumerator -


I have a requirement where I should know the calling method from GetEnumerator () .

I think the best method might possibly override the default behavior to GetEnumerator as one that gives me the GetEnumerator ([CallerMemberName] string caller = null ) But I can not seem to do anything that is still going on it originally.

  Public category MyItems: IEnumerable & lt; String & gt; {Private list & lt; String & gt; Item = new list & lt; String & gt; (); Public Maitum () {items.Add ("A"); Items.Add ("two"); Items.Add ("three"); Items.Add ("four"); Items.Add ("five"); Items.Add ("six"); } Public IEnumerator & lt; String & gt; GetEnumerator () {Return Items. GetEnumerator (); } Public IEnumerator & lt; String & gt; GetEnumerator ([CallerMemberName] string caller = null) {var method = caller; Return items Getannamator (); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {Throw New NotImplementedException); }}   

An example of some calling code can be

  1-click the private zeros button (object sender, event_arigues e) {MyItems items = new MyItems (); Objectives (different items in item) {}}   

The purpose is that I want to know in "button1_click" in GetEnumerator () for example < / Code> Method

I do not think it's possible that what you want to do, Code> foreach , in my knowledge, always calls without any arguments However, I see two possibilities in your problem

You To get calling method A can use one:

  public Aiananametr & lt; String & gt; GetEnumerator () {StackTrace stackTrace = new StackTrace (); Console.WriteLine (stackTrace.GetFrame (1) .GetMethod () name.); Return items Getannamator (); }   

or you can use another method instead of GetEnumerator () , which takes the [CallerMemberName] attribute.

  Public IEnumerable & lt; String & gt; Iterate ([CallerMemberName] string caller = null) {Console.WriteLine (collar); Return the goods; } In the foreach (var myItem items.Iterate ()) {// ..}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -