c# - Override the default behaviour of GetEnumerator -
I have a requirement where I should know the calling method from I think the best method might possibly override the default behavior to An example of some calling code can be The purpose is that I want to know in I do not think it's possible that what you want to do, Code> foreach , in my knowledge, always calls You To get calling method A can use one: or you can use another method instead of GetEnumerator () .
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); }}
1-click the private zeros button (object sender, event_arigues e) {MyItems items = new MyItems (); Objectives (different items in item) {}}
"button1_click" in GetEnumerator () for example < / Code> Method
public Aiananametr & lt; String & gt; GetEnumerator () {StackTrace stackTrace = new StackTrace (); Console.WriteLine (stackTrace.GetFrame (1) .GetMethod () name.); Return items Getannamator (); }
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
Post a Comment