当前位置: 首页>>代码示例>>C#>>正文


C# StackTrace.FirstOrDefault方法代码示例

本文整理汇总了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");
        }
开发者ID:chriscatilo,项目名称:docs.particular.net,代码行数:48,代码来源:EndpointNameHelper.cs

示例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(...)");
        }
开发者ID:nghead,项目名称:NServiceBus,代码行数:29,代码来源:DefaultEndpointName.cs

示例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;
            }
        }
开发者ID:ogdenmd,项目名称:NServiceBus,代码行数:58,代码来源:EndpointHelper.cs

示例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()))}");
        }
开发者ID:gitter-badger,项目名称:kino,代码行数:21,代码来源:ClusterMonitorSocketFactory.cs

示例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)";
        }
开发者ID:rohanbaraskar,项目名称:SeleniumAutomationFramework,代码行数:51,代码来源:SeleniumHelper.cs

示例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;
        }
开发者ID:authorunknown,项目名称:SpecFlow-Examples,代码行数:59,代码来源:ManualStepExtensions.cs


注:本文中的System.Diagnostics.StackTrace.FirstOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。