2

I have a requirement where I need to know the calling method to the GetEnumerator().

The best way I could think would be possibly overriding the default behaviour to GetEnumerator to one that I create i.e GetEnumerator([CallerMemberName]string caller = null) but I cannot seem to do this as anything calling it still goes to the original one.

public class MyItems : IEnumerable<string>
{
    private List<string> items = new List<string>();
    public MyItems()
    {
        items.Add("One");
        items.Add("Two");
        items.Add("Three");
        items.Add("Four");
        items.Add("Five");
        items.Add("Six");
    }

    public IEnumerator<string> GetEnumerator()
    {
        return items.GetEnumerator();
    }

    public IEnumerator<string> GetEnumerator([CallerMemberName]string caller = null)
    {
        var method = caller;
        return items.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

Example of some calling code could be

private void button1_click(object sender,EventArgs e)
{
    MyItems items = new MyItems();
    foreach (var item in items)
    {

    }
}

The aim is that I would want to know for example "button1_click" in the GetEnumerator() method

3
  • what does the caller code look like?
    – default
    Mar 21, 2013 at 11:06
  • Do not implement IEnumerable directly. Instead, have a method GetEnumerable(string caller) that returns an IEnumerable and take the caller as argument.
    – Guillaume
    Mar 21, 2013 at 11:07
  • Not implementing IEnumerable is not an option as we are implementing an interface that already implements IEnumerable. The interface is Microsoft IDBSet<T> interface. We need to know the calling method from a generic repositiory so that we can react accordingly but would like to keep the separation between the repository layer and the DBSets etc...
    – jclarkson
    Mar 21, 2013 at 11:35

2 Answers 2

2

I don't think it's possible to do exactly what you want to do, since foreach, to my knowledge, always calls the GetEnumerator() without any arguments. However, I see two possibilities to your issue

You can use a StackTrace to get the calling method:

public IEnumerator<string> GetEnumerator()
{
    StackTrace stackTrace = new StackTrace();
    Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
    return items.GetEnumerator();
}

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

public IEnumerable<string> Iterate([CallerMemberName]string caller = null)
{
    Console.WriteLine(caller);
    return items;
}

foreach (var myItem in items.Iterate())
{
    //..
}
3
  • 1
    The StackFrame approach works for foreach() but do note that in items.Where(...) it will give "Where" as the caller name.
    – H H
    Mar 21, 2013 at 11:39
  • @Guillaume do you mean the first method with the StackTrace?
    – default
    Mar 21, 2013 at 14:19
  • @Default yes, that's what I meant
    – Guillaume
    Mar 21, 2013 at 14:39
0

seems like , you need to use StackTrace Class

StackTrace st = new StackTrace();
        var fr = st.GetFrames();
        if(fr != null && fr.Any() &&fr.Count() >1)
        {
            MessageBox.Show(fr.ElementAt(1).GetMethod().Name);
        }
3
  • doesn't Any() imply that there are more than 0 elements? Do you really need to check for Count (and if you do, shouldn't it be >0)?
    – default
    Mar 21, 2013 at 12:13
  • Count() > 1 is used to prevent fr.ElementAt(1) if there is only one frame than it will throw exception Mar 21, 2013 at 12:16
  • so then, the Any() call is not needed
    – Lyra
    Feb 8, 2016 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.