Как найти рекурсию в Вашем приложении?

Вы можете использовать относительное связывание с поздним связыванием IDispatch в C #.

http://support.microsoft.com/kb/302902

Вот пример использования Excel. Таким образом, вам не нужно добавлять ненужную зависимость от распухшей Microsoft PIA:

//Create XL
Object xl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));

//Get the workbooks collection.
//   books = xl.Workbooks;
Object books = xl.GetType().InvokeMember( "Workbooks", 
      BindingFlags.GetProperty, null, xl, null);

//Add a new workbook.
//   book = books.Add();
Objet book = books.GetType().InvokeMember( "Add", 
      BindingFlags.InvokeMethod, null, books, null );

//Get the worksheets collection.
//   sheets = book.Worksheets;
Object sheets = book.GetType().InvokeMember( "Worksheets",
      BindingFlags.GetProperty, null, book, null );

Object[] parameters;

//Get the first worksheet.
//   sheet = sheets.Item[1]
parameters = new Object[1];
parameters[0] = 1;
Object sheet = sheets.GetType().InvokeMember( "Item", 
      BindingFlags.GetProperty, null, sheets, parameters );

//Get a range object that contains cell A1.
//   range = sheet.Range["A1];
parameters = new Object[2];
parameters[0] = "A1";
parameters[1] = Missing.Value;
Object range = sheet.GetType().InvokeMember( "Range",
      BindingFlags.GetProperty, null, sheet, parameters );

//Write "Hello, World!" in cell A1.
//   range.Value = "Hello, World!";
parameters = new Object[1];
parameters[0] = "Hello, World!";
objRange_Late.GetType().InvokeMember( "Value", BindingFlags.SetProperty, 
      null, range, parameters );

//Return control of Excel to the user.
//   xl.Visible = true;
//   xl.UserControl = true;
parameters = new Object[1];
parameters[0] = true;
xl.GetType().InvokeMember( "Visible", BindingFlags.SetProperty,
      null, xl, Parameters );
xl.GetType().InvokeMember( "UserControl", BindingFlags.SetProperty,
      null, xl, Parameters );

6
задан AngryHacker 5 November 2009 в 19:51
поделиться

6 ответов

В общем случае это вопрос, на который нет ответа. За исключением самых тривиальных примеров (например, функция, вызывающая сама себя), нет способа проанализировать программу и определить, происходит ли рекурсия. Вам просто нужно будет запустить отладчик или другие инструменты среды выполнения.

Это пример проблемы остановки .

6
ответ дан 8 December 2019 в 05:55
поделиться

A recursion is not easy to find in some situations like:

method1() {
  method2()
}

method2() {
  method1()
}

So a regex probably would not help you to find it unless it's a trivial case.

5
ответ дан 8 December 2019 в 05:55
поделиться

I agree a regexp isn't going to cut it here.

A more direct way would be to get a dump file and look at it to see where the exception was thrown.

Or you could look at a static analysis tool like NDepend to examine the programs flow.

4
ответ дан 8 December 2019 в 05:55
поделиться

How about using a profiling tool like RedGate's Ants profiler or dotTrace?

They both offer free trials. Just run the code with the profiler running and it will quickly show you where your time/memory is being spent.

I'd bet that your problem recursive function will stick out quite a bit.

Additionally, what error logging framework are you using? In case the answer is none, consider adopting one. This Question deals with the options. With a good system, you should be able to get the stack trace which, if you're lucky, may give you clues as to where the exception is occurring.

4
ответ дан 8 December 2019 в 05:55
поделиться

Подключите к службу в отладчике и отлаживайте ее должным образом. Вы обнаружите, что это намного проще, чем пытаться искать код любого проекта разумного размера.

2
ответ дан 8 December 2019 в 05:55
поделиться

Самый простой способ сделать это - получить трассировку стека того, что дает сбой. Трассировка стека будет выглядеть так:

Blah
Foo
Baz
Hello
...
Frob
Frob
Frob
Frob
[several hundred more Frobs]
Frob
Frob
...
Frob
Something -- crash!

"Frob" - это рекурсивная функция. : -)

1
ответ дан 8 December 2019 в 05:55
поделиться
Другие вопросы по тегам:

Похожие вопросы: