Commits

Inside Zhou committed b80b535 Merge

Merge

Comments (0)

Files changed (8)

IListExtension.cs

-using System;
-using System.Collections;
-using System.Collections.Generic;
-
-namespace Inside.Util {
-	/// <summary>
-	/// 为集合提供扩展方法。
-	/// </summary>
-	public static class IListExtension {
-		/// <summary>
-		/// 从集合中随机选择元素。
-		/// </summary>
-		public static T[] RandomSelect<T>(this IList src, int count = 6) {
-			if(null == src) {
-				throw new ArgumentNullException("src");
-			}
-
-			if(src.Count <= count) {
-				throw new ArgumentException("集合的长度必须大于选择的个数。", "count");
-			}
-
-			var result = new List<T>();
-			for(int i = 0;i < count;++i) {
-				result.Add((T)src[Math.Abs(Guid.NewGuid().GetHashCode()) % src.Count]);
-			}
-
-			return result.ToArray();
-		}
-
-		/// <summary>
-		/// 打乱集合中元素的次序。
-		/// </summary>
-		/// PDL:
-		/// 从尾部开始遍历该集合;
-		/// 每访问一个索引,将其中的元素随机地与剩余索引中的元素交换一次;
-		/// 访问至集合头部的元素时停止。
-		public static void Shuffle(this IList src) {
-			if(null == src || 0 == src.Count) {
-				return;
-			}
-
-			Object tmpForSwap = null;
-			int tmpCursorIndex = 0;
-
-			for(int cursorIndex = src.Count - 1;0 != cursorIndex;--cursorIndex) {
-				/* 生成一个区间在[0.0, 1.0)的随机数,
-				 * 将其与光标索引相乘得到位于[0, cursorIndex)区间的一个随机数。
-				 */
-				tmpCursorIndex = Math.Abs(Guid.NewGuid().GetHashCode()) % cursorIndex;
-
-				tmpForSwap = src[cursorIndex];
-				src[cursorIndex] = src[tmpCursorIndex];
-				src[tmpCursorIndex] = tmpForSwap;
-			}
-		}
-	}
-}

Inside.Util.csproj

   <ItemGroup>
     <Compile Include="ByteExtension.cs" />
     <Compile Include="DyObj.cs" />
-    <Compile Include="IListExtension.cs" />
+    <Compile Include="ListExtension.cs" />
     <Compile Include="Json\ByteArrayConverter.cs" />
     <Compile Include="Json\DBNullConverter.cs" />
     <Compile Include="Json\DecimalConverter.cs" />
     <Compile Include="Json\JsonHelper.cs" />
     <Compile Include="Json\SimpleDateTimeConverter.cs" />
     <Compile Include="MiscExtension.cs" />
+    <Compile Include="ObjectExtension.cs" />
     <Compile Include="ObjectHelper.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Sql\Condition.cs" />
     <Compile Include="Web\HttpRequestBaseExtension.cs" />
     <Compile Include="Web\MIME.cs" />
     <Compile Include="Web\MvcControllerBase.cs" />
+    <Compile Include="Web\PriviledgeDescriptionAttribute.cs" />
     <Compile Include="Web\UriExtension.cs" />
     <Compile Include="Web\ViewAsFileResult.cs" />
     <Compile Include="Web\WSHelper.cs" />
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Inside.Util {
+	public static class ListExtension {
+		/// <summary>
+		/// 从列表中随机选择元素。
+		/// </summary>
+		public static T[] RandomSelect<T>(this IList src, int count = 6) {
+			if(null == src) {
+				throw new ArgumentNullException("src");
+			}
+
+			if(src.Count <= count) {
+				throw new ArgumentException("集合的长度必须大于选择的个数。", "count");
+			}
+
+			var result = new List<T>();
+			for(int i = 0;i < count;++i) {
+				result.Add((T)src[Math.Abs(Guid.NewGuid().GetHashCode()) % src.Count]);
+			}
+
+			return result.ToArray();
+		}
+
+		/// <summary>
+		/// 打乱列表中元素的次序。
+		/// </summary>
+		public static void Shuffle(this IList src) {
+			if(null == src || 0 == src.Count) {
+				return;
+			}
+
+			Object tmpForSwap = null;
+			int tmpCursorIndex = 0;
+
+			for(int cursorIndex = src.Count - 1;0 != cursorIndex;--cursorIndex) {
+				tmpCursorIndex = Math.Abs(Guid.NewGuid().GetHashCode()) % cursorIndex;
+
+				tmpForSwap = src[cursorIndex];
+				src[cursorIndex] = src[tmpCursorIndex];
+				src[tmpCursorIndex] = tmpForSwap;
+			}
+		}
+	}
+}
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 
 namespace Inside.Util {
 
 			return result;
 		}
+
+		/// <summary>
+		/// 获取第一个类型为T的Attribute,若无则返回null。
+		/// </summary>
+		public static T GetAttribute<T>(this MethodInfo src) where T : Attribute {
+			return (T)src.GetCustomAttributes(false).FirstOrDefault((attr) => { return attr is T; });
+		}
 	}
 }

ObjectExtension.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Inside.Util {
+	public static class ObjectExtension {
+		public static bool IsNil(this object src) {
+			return ObjectHelper.IsNil(src);
+		}
+	}
+}

Web/AuthorizeExAttribute.cs

 			}
 		}
 	}
-}
+}

Web/MvcControllerBase.cs

 using System.Diagnostics;
 using System.Linq;
 using System.Text.RegularExpressions;
+using System.Reflection;
+using System.Web.Compilation;
 using System.Web;
 using System.Web.Security;
 using System.Web.Mvc;
 
 namespace Inside.Util.Web {
 	public abstract class MvcControllerBase : Controller {
-		protected bool Debugging { get; private set; }
+		public static List<Type> Controllers {
+			get {
+				var types = new List<Type>();
+
+				foreach(Assembly assembly in BuildManager.GetReferencedAssemblies()) {
+					foreach(var t in assembly.GetTypes()) {
+						if(t != null
+							&& t.IsPublic
+							&& t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
+							&& !t.IsAbstract
+							&& typeof(IController).IsAssignableFrom(t)) {
+
+							types.Add(t);
+						}
+					}
+				}
+
+				return types;
+			}
+		}
+
+		public static List<MethodInfo> Actions {
+			get {
+				var methods = new List<MethodInfo>();
+				foreach(var c in Controllers) {
+					methods.AddRange(c.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod).Where((m) => {
+						return m.ReturnType == typeof(ActionResult) || m.ReturnType.IsInstanceOfType(typeof(ActionResult));
+					}));
+				}
+
+				return methods;
+			}
+		}
+
+		public bool Debugging { get; private set; }
 
 		dynamic _useragent;
-		protected dynamic UserAgent {
+		public dynamic UserAgent {
 			get {
 				if(null == this._useragent) {
 					this._useragent = new DyObj();
 			this.Debugging = requestContext.HttpContext.IsDebuggingEnabled;
 		}
 
-		protected override void OnActionExecuted(ActionExecutedContext filterContext) {
-			base.OnActionExecuted(filterContext);
-
-			if(!filterContext.Canceled) {
-				this.ViewBag.useragent = this.UserAgent;
-				this.ViewBag.debugging = this.Debugging;
-			}
-		}
-
 		protected ActionResult ViewAsFile(string view_name, string content_type, object model, string file_download_name, string master_name = null) {
 			if(null != model) {
 				this.ViewData.Model = model;

Web/PriviledgeDescriptionAttribute.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Inside.Util.Web {
+	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
+	public class PriviledgeDescriptionAttribute : Attribute {
+		public string ID { get; private set; }
+		public string Name { get; private set; }
+		public string Description { get; set; }
+
+		int _state = 0;
+		public int State {
+			get {
+				return this._state;
+			}
+			set {
+				this._state = value;
+			}
+		}
+
+		public PriviledgeDescriptionAttribute(string id, string name) {
+			if(string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name)) {
+				throw new ArgumentNullException();
+			}
+
+			this.ID = id;
+			this.Name = name;
+		}
+	}
+}