我想在下面写一个代码。是否可以在void方法中调用方法。我不想为所有方法一次又一次地编写静态部分,因为只是运行方法部分不同,其他部分相同。在C#中可以吗?
RunCodeInThisMethod(DoSomethingInDB()); // I want to call like this
private void RunCodeInThisMethod (Method method)
{
Connection conoc = null;
Command oCmd = null;
try
{
conoc = new GetConnection("blabla", "blabla");
oCmd = new Command();
oCmd.Connection = conoc;
using (TransactionScope scope = new TransactionScope())
{
// RUN method this part
scope.Complete();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
oCmd?.Dispose();
conoc?.Close();
conoc.Dispose();
}
}