//判断是否配置了方面的拦截 private bool checkCanInjection(IMessage msg) { System.Reflection.MethodBase method = null; if (msg is IConstructionCallMessage) { IConstructionCallMessage ccm = (IConstructionCallMessage)msg; method = ccm.MethodBase; } else { IMethodCallMessage callmsg = (IMethodCallMessage)msg; method = callmsg.MethodBase; } object[] pros = method.GetCustomAttributes(typeof(InjectionMethodSwitchAttribute), false); InjectionMethodSwitchAttribute att = null; if (pros != null && pros.Length > 0) { att = pros[0] as InjectionMethodSwitchAttribute; } if (att == null) { return(true); } else { return(att.AspectManaged); } }
// From http://geekswithblogs.net/ajohns/archive/2004/08/05/9389.aspx static bool IsRunningUnderNUnit() { System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(); for (int i = 0; i < trace.FrameCount; i++) { System.Reflection.MethodBase methodBase = trace.GetFrame(i).GetMethod(); /*if (methodBase.IsDefined(typeof(NUnit.Framework.TestAttribute), true)) { * return true; * }*/ object[] attrArr = methodBase.GetCustomAttributes(false); foreach (object attr in attrArr) { if (attr.GetType().Name == "TestAttribute") { return(true); } if (attr.GetType().Name == "TestFixtureSetUpAttribute") { return(true); } } } return(false); }
static string GetStackSig() { StackTrace stack = new StackTrace(); StackFrame[] frames = stack.GetFrames(); StringBuilder stackSig = new StringBuilder(); for (int x = STACK_WALK; x < frames.Length; x++) { StackFrame f = frames[x]; System.Reflection.MethodBase method = f.GetMethod(); string assembly = method.DeclaringType.Assembly.FullName.Split(',')[0]; // Look for [NoCache] attributes object[] attributes = method.GetCustomAttributes(true); foreach (Attribute a in attributes) { // If found, return a null sig if (a.GetType() == typeof(NoCacheAttribute)) { Log.Debug(string.Format("CACHEMATRIX: [NoCache] block produced by {0}.{1}.{2}()", assembly, method.DeclaringType.Name, method.Name)); return(null); } } if (!IGNORE_ASSEMBLIES_IN_SIG.Contains(assembly) && !assembly.StartsWith(IGNORE_ASSEMBLIES_IN_SIG2)) { stackSig.Append(string.Format("{0}.{1}.{2}():", assembly, method.DeclaringType.Name, method.Name)); } } return(stackSig.ToString()); }
/// <summary> /// Initializes the current context and sets various members to the values supplied in parameters /// Also checks whether the app token is valid and whether the current user is authorized to access the called web method /// </summary> /// <param name="appToken">Application token supplied by the client</param> /// <param name="userToken">User token supplied by the client</param> /// <param name="userName">Current user name</param> /// <param name="failInNoApp">Whether to throw an exception if no approved application is found to match the token</param> /// public static void Initialize(string appToken, string userToken) { if (Current != null) { return; } using (ApplicationDataContext dataContext = new ApplicationDataContext()) { Current = new BrokerContext(); Current.ActivityId = Guid.NewGuid(); Current.ApplicationToken = appToken; Current.UserToken = userToken; Current.UserName = Security.CurrentUser; Application currentApplication = dataContext.Applications.SingleOrDefault(app => app.Token == appToken && app.IsApproved == true); if (currentApplication != null) { Current.ApplicationId = currentApplication.ApplicationId; Current.ApplicationName = currentApplication.Name; } else { throw new InvalidTokenException(appToken, userToken); } // Now find if current user is authorized to access the current method if (System.Web.HttpContext.Current != null) { var ctx = System.Web.HttpContext.Current; // Search up the stack to find the Method's message name System.Reflection.MethodBase method = null; for (int i = 1; true; i++) { StackFrame stackFrame = new StackFrame(i); method = stackFrame.GetMethod(); if (method == null || method.IsDefined(typeof(WebMethodAttribute), false) && method.DeclaringType.IsSubclassOf(typeof(System.Web.Services.WebService))) { break; } } if (method != null) { // Now get the method's message name WebMethodAttribute webMethodAttribute = (from attr in method.GetCustomAttributes(typeof(WebMethodAttribute), false) select attr).First() as WebMethodAttribute; Current.WebMethodMessageName = string.IsNullOrEmpty(webMethodAttribute.MessageName) ? method.Name : webMethodAttribute.MessageName; bool isAuthorized = System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal( string.Format("{0}/{1}", System.Web.HttpContext.Current.Request.Path, Current.WebMethodMessageName), System.Web.HttpContext.Current.User, "GET" ); if (!isAuthorized) { Console.Write("not authoriced exception" + Current.WebMethodMessageName); throw new System.Security.Authentication.AuthenticationException(); } } } } }
protected void Logging() { StackTrace trace = new System.Diagnostics.StackTrace(); StackFrame[] frames = trace.GetFrames(); //find who is calling me System.Reflection.MethodBase method = frames[1].GetMethod(); KerberosTestAttribute.Site = this.Site; object[] attrs = method.GetCustomAttributes(false); if (attrs == null) { return; } foreach (object o in attrs) { //for out test attribute, invoke "Logging" method, for MSTEST attributes, do accordingly Type thisType = o.GetType(); switch (thisType.Name) { case "DescriptionAttribute": { Site.Log.Add(LogEntryKind.Comment, "Test description: " + typeof(DescriptionAttribute).GetProperty("Description").GetValue(o, null).ToString()); } break; case "PriorityAttribute": { Site.Log.Add(LogEntryKind.Comment, "Implementation priority of this test case is: " + typeof(PriorityAttribute).GetProperty("Priority").GetValue(o, null).ToString()); } break; case "TestCategoryAttribute": { IList <string> categories = (IList <string>) typeof(TestCategoryAttribute).GetProperty("TestCategories").GetValue(o, null); if (categories.Count > 0) { Site.Log.Add(LogEntryKind.Comment, "This test case is belong to: " + categories[0] + "Test Category"); } } break; default: if (thisType.BaseType == typeof(KerberosTestAttribute)) { try { thisType.GetMethod("Logging").Invoke(o, null); } catch (Exception e) { KerberosTestAttribute.Site.Assert.Fail(e.InnerException.Message); } } break; } } }
public override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo) { target = method.DeclaringType.GetMethod(method.Name, new Type[] { typeof(Dictionary <string, string>) }); foreach (Attribute a in method.GetCustomAttributes(false)) { if (a is SpecialAttribute) { values.Add(((SpecialAttribute)a).Key, ((SpecialAttribute)a).Value); } } }
public override bool CompileTimeValidate(System.Reflection.MethodBase method) { // 1. 检查方法上是否有添加排除跟踪的Attribute标记 var array = method.GetCustomAttributes(typeof(NoProfilerTraceAttribute), false); if (array != null && array.Length > 0 && array[0] is NoProfilerTraceAttribute) { return(false); } // 2. 检查定义方法的类型上是否有添加排除跟踪的Attribute标记 array = method.DeclaringType.GetCustomAttributes(typeof(NoProfilerTraceAttribute), false); if (array != null && array.Length > 0 && array[0] is NoProfilerTraceAttribute) { return(false); } return(true); }
static string GetTestCaseName(bool fullName) { System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(); for (int i = 0; i < stackTrace.FrameCount; i++) { System.Reflection.MethodBase method = stackTrace.GetFrame(i).GetMethod(); object[] testAttrs = method.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), false); if (testAttrs != null && testAttrs.Length > 0) { if (fullName) { return(method.DeclaringType.FullName + "." + method.Name); } else { return(method.Name); } } } return("GetTestCaseName[UnknownTestMethod]"); }
internal static Exception Make_Exception_External(Exception ex) { int skip = 0; var stack = new StackTrace(); StackFrame[] Frames = stack.GetFrames(); for (int i = 0; i < Frames.Length; i++) { StackFrame Frame = Frames[i]; System.Reflection.MethodBase method = Frame.GetMethod(); object[] attr = method.GetCustomAttributes(typeof(LoggingMethod), true); if (!ReferenceEquals(attr, null) && attr.Length > 0) { skip++; } else {// stop at the first method that ISNT one we wanna skip, we only skip at the beginning of the stack break; } } return(ex.SetStackTrace(new StackTrace(skip, true))); }
/** <summary> * A more general version of GetRuleInvocationStack where you can * pass in the StackTrace of, for example, a RecognitionException * to get it's rule stack trace. * </summary> */ public static IList <string> GetRuleInvocationStack(StackTrace trace) { if (trace == null) { throw new ArgumentNullException("trace"); } List <string> rules = new List <string>(); StackFrame[] stack = trace.GetFrames() ?? new StackFrame[0]; for (int i = stack.Length - 1; i >= 0; i--) { StackFrame frame = stack[i]; MethodBase method = frame.GetMethod(); GrammarRuleAttribute[] attributes = (GrammarRuleAttribute[])method.GetCustomAttributes(typeof(GrammarRuleAttribute), true); if (attributes != null && attributes.Length > 0) { rules.Add(attributes[0].Name); } } return(rules); }
public static void Check() { StackTrace trace = new System.Diagnostics.StackTrace(); StackFrame[] frames = trace.GetFrames(); //find who is calling me System.Reflection.MethodBase method = frames[1].GetMethod(); DrsrTestAttribute.Site = EnvironmentConfig.TestSite; object[] attrs = method.GetCustomAttributes(false); if (attrs == null) { return; } string level = null; foreach (object o in attrs) { //for out test attribute, invoke "Do" method, for MSTEST attributes, do accordingly Type thisType = o.GetType(); switch (thisType.Name) { case "DescriptionAttribute": { EnvironmentConfig.TestSite.Log.Add(LogEntryKind.Comment, "Test description: " + typeof(DescriptionAttribute).GetProperty("Description").GetValue(o, null).ToString()); } break; case "PriorityAttribute": { EnvironmentConfig.TestSite.Log.Add(LogEntryKind.Comment, "Implementation priority of this scenario is: " + typeof(PriorityAttribute).GetProperty("Priority").GetValue(o, null).ToString()); } break; case "TestCategoryAttribute": { TestCategoryAttribute tca = o as TestCategoryAttribute; foreach (string s in tca.TestCategories) { if (s.StartsWith("Win")) { level = s; continue; } // Check if SDC, RODC exist when TestCategory contain SDC, RODC if (s.Equals("SDC")) { if (!EnvironmentConfig.MachineStore.ContainsKey(EnvironmentConfig.Machine.WritableDC2)) { EnvironmentConfig.TestSite.Assume.Fail("The test requires a Secondary writable DC in the environment. Please set the corresponding field in PTF config, or make sure the machine can be connected."); } continue; } if (s.Equals("RODC")) { if (!EnvironmentConfig.MachineStore.ContainsKey(EnvironmentConfig.Machine.RODC)) { EnvironmentConfig.TestSite.Assume.Fail("The test requires a Read-Only DC in the environment. Please set the corresponding field in PTF config, or make sure the machine can be connected."); } continue; } } } break; default: if (thisType.BaseType == typeof(DrsrTestAttribute)) { try { thisType.GetMethod("Do").Invoke(o, null); } catch (Exception e) { DrsrTestAttribute.Site.Assert.Fail(e.InnerException.Message); } } break; } } if (level == null) { throw new Exception("Test Case not set in any domain functional level category"); } FunctionLevelAttribute fl = null; switch (level) { case "Win2000": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2000); break; case "Win2003": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2003); break; case "Win2008": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2008); break; case "Win2008R2": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2008R2); break; case "Win2012": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2012); break; case "Win2012R2": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WIN2012R2); break; case "WinThreshold": fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WINTHRESHOLD); break; case "Winv1803": ADCommonServerAdapter adapter = new ADCommonServerAdapter(); adapter.Initialize(DrsrTestAttribute.Site); ServerVersion curVersion = adapter.PDCOSVersion; if (curVersion < ServerVersion.Winv1803) { DrsrTestAttribute.Site.Assert.Inconclusive("Test case expects PDCOSVersion {0} should be Equal or Greater than Winv1803", curVersion); } fl = new FunctionLevelAttribute(DrsrDomainFunctionLevel.DS_BEHAVIOR_WINTHRESHOLD); break; default: throw new Exception("Unknown domain functional level category: " + level); } fl.Do(); }