本文整理汇总了C#中System.Diagnostics.StackTrace.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# StackTrace.FirstOrDefault方法的具体用法?C# StackTrace.FirstOrDefault怎么用?C# StackTrace.FirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Diagnostics.StackTrace
的用法示例。
在下文中一共展示了StackTrace.FirstOrDefault方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEntryType
static Type GetEntryType()
{
var stackTraceToExamine = new StackTrace();
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly?.EntryPoint != null)
{
return entryAssembly.EntryPoint.ReflectedType;
}
StackFrame targetFrame = null;
var stackFrames = new StackTrace().GetFrames();
if (stackFrames != null)
{
targetFrame =
stackFrames.FirstOrDefault(
f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
}
if (targetFrame != null)
{
return targetFrame.GetMethod().ReflectedType;
}
stackFrames = stackTraceToExamine.GetFrames();
if (stackFrames != null)
{
targetFrame =
stackFrames.FirstOrDefault(
f =>
{
var declaringType = f.GetMethod().DeclaringType;
return declaringType != typeof(EndpointConfiguration);
});
}
if (targetFrame == null)
{
targetFrame = stackFrames.FirstOrDefault(
f => f.GetMethod().DeclaringType.Assembly != typeof(EndpointConfiguration).Assembly);
}
if (targetFrame != null)
{
return targetFrame.GetMethod().ReflectedType;
}
throw new Exception("Could not derive EndpointName");
}
示例2: Get
/// <summary>
/// Gets the name of this endpoint
/// </summary>
/// <returns></returns>
public static string Get()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null && entryAssembly.EntryPoint != null)
{
return entryAssembly.EntryPoint.ReflectedType.Namespace ??
entryAssembly.EntryPoint.ReflectedType.Assembly.GetName().Name;
}
var stackFrames = new StackTrace().GetFrames();
StackFrame targetFrame = null;
if (stackFrames != null)
{
targetFrame =
stackFrames.FirstOrDefault(
f => typeof (HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
}
if (targetFrame != null)
return targetFrame.GetMethod().ReflectedType.Namespace ??
targetFrame.GetMethod().ReflectedType.Assembly.GetName().Name;
throw new InvalidOperationException(
"No endpoint name could be generated, please specify your own convention using Configure.DefineEndpointName(...)");
}
示例3: Initialize
void Initialize()
{
if (initialized)
return;
try
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null && entryAssembly.EntryPoint != null)
{
entryType = entryAssembly.EntryPoint.ReflectedType;
return;
}
StackFrame targetFrame = null;
var stackFrames = new StackTrace().GetFrames();
if (stackFrames != null)
{
targetFrame =
stackFrames.FirstOrDefault(
f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
}
if (targetFrame != null)
{
entryType = targetFrame.GetMethod().ReflectedType;
return;
}
if (stackTraceToExamine != null)
{
stackFrames = stackTraceToExamine.GetFrames();
if (stackFrames != null)
{
targetFrame =
stackFrames.FirstOrDefault(
f =>
{
var declaringType = f.GetMethod().DeclaringType;
return declaringType != typeof(Configure) && declaringType != typeof(BusConfiguration);
});
}
}
if (targetFrame == null)
targetFrame = stackFrames.FirstOrDefault(
f => f.GetMethod().DeclaringType.Assembly != typeof(Configure).Assembly);
if (targetFrame != null)
{
entryType = targetFrame.GetMethod().ReflectedType;
}
}
finally
{
initialized = true;
}
}
示例4: GetSocketPurpose
private SocketPurpose GetSocketPurpose()
{
var stackFrames = new StackTrace(1).GetFrames();
if (stackFrames
.FirstOrDefault(sf => sf.GetMethod().Name == CreateClusterMonitorSendingSocketMethod) != null)
{
return SocketPurpose.ClusterMonitorSendingSocket;
}
if (stackFrames
.FirstOrDefault(sf => sf.GetMethod().Name == CreateClusterMonitorSubscriptionSocketMethod) != null)
{
return SocketPurpose.ClusterMonitorSubscriptionSocket;
}
if (stackFrames
.FirstOrDefault(sf => sf.GetMethod().Name == CreateRouterCommunicationSocketMethod) != null)
{
return SocketPurpose.RouterCommunicationSocket;
}
throw new Exception($"Socket creation method is unexpected: {string.Join(" @ ", stackFrames.Select(sf => sf.ToString()))}");
}
示例5: GetCurrentPositionText
public static string GetCurrentPositionText()
{
try
{
var frames = new StackTrace(true).GetFrames();
if (frames != null)
{
var featureFileFrame = frames.FirstOrDefault(f =>
{
var fileName = f.GetFileName();
return fileName != null && fileName.EndsWith(".feature");
});
if (featureFileFrame != null)
{
var lines = File.ReadAllLines(featureFileFrame.GetFileName());
const int frameSize = 20;
var currentLine = featureFileFrame.GetFileLineNumber() - 1;
var minLine = Math.Max(0, currentLine - frameSize);
var maxLine = Math.Min(lines.Length - 1, currentLine + frameSize);
for (var lineNo = currentLine - 1; lineNo >= minLine; lineNo--)
{
if (!lines[lineNo].TrimStart().StartsWith("Scenario:")) continue;
minLine = lineNo + 1;
break;
}
for (var lineNo = currentLine + 1; lineNo <= maxLine; lineNo++)
{
if (!lines[lineNo].TrimStart().StartsWith("Scenario:")) continue;
maxLine = lineNo - 1;
break;
}
for (var lineNo = minLine; lineNo <= maxLine; lineNo++)
{
if (lineNo == currentLine)
{
return string.Format("->" + lines[lineNo]);
}
}
}
}
}
catch (Exception)
{
}
return "(Unable to detect current step)";
}
示例6: GetCurrentPositionText
private static int GetCurrentPositionText(StringBuilder messageBoxText)
{
int currentPositionText = 0;
try
{
var frames = new StackTrace(true).GetFrames();
if (frames != null)
{
var featureFileFrame = frames.FirstOrDefault(f =>
f.GetFileName() != null &&
f.GetFileName().EndsWith(".feature"));
if (featureFileFrame != null)
{
var lines = File.ReadAllLines(featureFileFrame.GetFileName());
const int frameSize = 20;
int currentLine = featureFileFrame.GetFileLineNumber() - 1;
int minLine = Math.Max(0, currentLine - frameSize);
int maxLine = Math.Min(lines.Length - 1, currentLine + frameSize);
for (int lineNo = currentLine - 1; lineNo >= minLine; lineNo--)
{
if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
{
minLine = lineNo + 1;
break;
}
}
for (int lineNo = currentLine + 1; lineNo <= maxLine; lineNo++)
{
if (lines[lineNo].TrimStart().StartsWith("Scenario:"))
{
maxLine = lineNo - 1;
break;
}
}
for (int lineNo = minLine; lineNo <= maxLine; lineNo++)
{
messageBoxText.Append(lineNo == currentLine ? "-> " : " ");
messageBoxText.AppendLine(lines[lineNo]);
if (lineNo == currentLine)
currentPositionText = lineNo - minLine;
}
return currentPositionText;
}
}
}
catch(Exception ex)
{
Debug.WriteLine(ex, "GetCurrentPositionText");
}
messageBoxText.AppendLine("(Unable to detect current step)");
return currentPositionText;
}