简体   繁体   中英

Why isn't my method being called?

I'm running a C# project compiled against .NET 3.5 in VS 2010 SP 1 on a Windows 8 machine with .NET 4.5 installed, if that matters.

I have a method that looks like the following:

private IEnumerable<TModel> ExecuteAndGet<TModel>(string name, IEnumerable<SqlParameter> parameters)
        where TModel : new()
{ // <-- Breakpoint 1

    // Non-trivial code so I doubt the method call is being optimized away
    // Or something if that's even possible.
}

In the same class in a different method I call ExecuteAndGet like so:

this.ExecuteAndGet<object>("[dbo].[SomeStoredProcedure]", parameters); // <-- Breakpoint 2

That's definitely the only overload of that method. While running the program in debug mode I hit breakpoint 2. I've tried just regular "F5" and "Step INTO" but it completely skips going inside the method altogether and it skips breakpoint 1. I have no idea why this is happening. Other calls to ExecuteAndGet are working and I don't see why this is any different. Help, please?

Thanks.

You're not actually enumerating. Try:

this.ExecuteAndGet<object>("[dbo].[SomeStoredProcedure]", parameters).ToList();

The enumerable returned is implemented as an iterator block. Code inside iterator blocks does not actually execute until they are enumerated for the first time, eg add call .ToArray() or .ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM