/// <summary> /// 根据类型获取类的实例 /// </summary> /// <param name="type">可以是接口类型(带有AjaxClassAttribute标记),或者是一个类的类型</param> /// <returns></returns> public static object GetInstance(Type type) { if (type == null) { return(null); } if (type.IsInterface) { if (type.IsDefined(typeof(AjaxClassAttribute), true)) { object[] attrs = type.GetCustomAttributes(typeof(AjaxClassAttribute), true); if (attrs.Length > 0 && (attrs[0] is AjaxClassAttribute)) { AjaxClassAttribute attribute = attrs[0] as AjaxClassAttribute; Type classType = Type.GetType(attribute.DeclaringType); if (classType != null) { return(Activator.CreateInstance(classType)); } } } } else if (type.IsClass) { return(Activator.CreateInstance(type)); } return(null); }
private static void SetAjaxMethod(AjaxMethod method, MethodInfo methodInfo) { if (!methodInfo.DeclaringType.IsDefined(typeof(AjaxClassAttribute))) { throw new Exception(string.Format("无效的类[{0}]", methodInfo.DeclaringType)); } if (!methodInfo.IsDefined(typeof(AjaxMethodAttribute), true)) { throw new Exception(string.Format("方法[{0}]不是AjaxMethod标记方法", methodInfo.Name)); } method.DeclaringType = methodInfo.DeclaringType; method.MethodInfo = methodInfo; method.ReturnType = methodInfo.ReturnType; AjaxClassAttribute attr = methodInfo.DeclaringType.GetCustomAttribute <AjaxClassAttribute>(true); method.FullClassName = attr.DeclaringType; if (methodInfo.IsDefined(typeof(DescriptionAttribute), true)) { method.Description = methodInfo.GetCustomAttribute <DescriptionAttribute>(true).Description; } }