diff --git a/NewLife.Core/Log/CompositeLog.cs b/NewLife.Core/Log/CompositeLog.cs
index 0d9d13c..f4db86b 100644
--- a/NewLife.Core/Log/CompositeLog.cs
+++ b/NewLife.Core/Log/CompositeLog.cs
@@ -36,7 +36,7 @@ namespace NewLife.Log
/// <summary>删除日志提供者</summary>
/// <param name="log"></param>
/// <returns></returns>
- public CompositeLog Remove(ILog log) { if (Logs.Contains(log))Logs.Remove(log); return this; }
+ public CompositeLog Remove(ILog log) { if (Logs.Contains(log)) Logs.Remove(log); return this; }
/// <summary>写日志</summary>
/// <param name="level"></param>
@@ -77,16 +77,6 @@ namespace NewLife.Log
return null;
}
- //public ILog Get(Type type)
- //{
- // foreach (var item in Logs)
- // {
- // if (item != null && type.IsAssignableFrom(item.GetType())) return item;
- // }
-
- // return null;
- //}
-
/// <summary>已重载。</summary>
/// <returns></returns>
public override String ToString()
diff --git a/NewLife.Core/Model/IPlugin.cs b/NewLife.Core/Model/IPlugin.cs
index a706d91..8090948 100644
--- a/NewLife.Core/Model/IPlugin.cs
+++ b/NewLife.Core/Model/IPlugin.cs
@@ -152,9 +152,7 @@ namespace NewLife.Model
{
if (serviceType == typeof(PluginManager)) return this;
- if (Provider != null) Provider.GetService(serviceType);
-
- return null;
+ return Provider?.GetService(serviceType);
}
#endregion
}
diff --git a/NewLife.Core/Model/ModelExtension.cs b/NewLife.Core/Model/ModelExtension.cs
index 87911af..cd5796c 100644
--- a/NewLife.Core/Model/ModelExtension.cs
+++ b/NewLife.Core/Model/ModelExtension.cs
@@ -1,4 +1,5 @@
using System;
+using NewLife.Reflection;
namespace System
{
@@ -11,7 +12,12 @@ namespace System
/// <returns></returns>
public static T GetService<T>(this IServiceProvider provider)
{
- return (T)provider?.GetService(typeof(T));
+ if (provider == null) return default(T);
+
+ // 服务类是否当前类的基类
+ if (provider.GetType().As<T>()) return (T)provider;
+
+ return (T)provider.GetService(typeof(T));
}
}
diff --git a/NewLife.Core/Reflection/AssemblyX.cs b/NewLife.Core/Reflection/AssemblyX.cs
index 917803a..72fbe51 100644
--- a/NewLife.Core/Reflection/AssemblyX.cs
+++ b/NewLife.Core/Reflection/AssemblyX.cs
@@ -317,8 +317,7 @@ namespace NewLife.Reflection
foreach (var item in Types)
{
if (item.IsInterface || item.IsAbstract || item.IsGenericType) continue;
- //if (baseType != item && baseType.IsAssignableFrom(item)) list.Add(item);
- if (item.IsSubOf(baseType)) list.Add(item);
+ if (item != baseType && item.As(baseType)) list.Add(item);
}
if (list.Count <= 0) list = null;
diff --git a/NewLife.Core/Reflection/IReflect.cs b/NewLife.Core/Reflection/IReflect.cs
index 99131e3..fa68ebd 100644
--- a/NewLife.Core/Reflection/IReflect.cs
+++ b/NewLife.Core/Reflection/IReflect.cs
@@ -149,11 +149,11 @@ namespace NewLife.Reflection
#endregion
#region 插件
- /// <summary>是否子类</summary>
+ /// <summary>是否能够转为指定基类</summary>
/// <param name="type"></param>
/// <param name="baseType"></param>
/// <returns></returns>
- Boolean IsSubOf(Type type, Type baseType);
+ Boolean As(Type type, Type baseType);
/// <summary>在指定程序集中查找指定基类或接口的所有子类实现</summary>
/// <param name="asm">指定程序集</param>
@@ -577,7 +577,7 @@ namespace NewLife.Reflection
{
if (type.HasElementType) return type.GetElementType();
- if (typeof(IEnumerable).IsAssignableFrom(type))
+ if (type.As<IEnumerable>())
{
// 如果实现了IEnumerable<>接口,那么取泛型参数
foreach (var item in type.GetInterfaces())
@@ -598,11 +598,8 @@ namespace NewLife.Reflection
/// <returns></returns>
public virtual Object ChangeType(Object value, Type conversionType)
{
- //return Convert.ChangeType(value, conversionType);
-
Type vtype = null;
if (value != null) vtype = value.GetType();
- //if (vtype == conversionType || conversionType.IsAssignableFrom(vtype)) return value;
if (vtype == conversionType) return value;
if (conversionType.IsEnum)
@@ -621,7 +618,7 @@ namespace NewLife.Reflection
{
value = str.TrimStart(new Char[] { '$', '¥' });
}
- else if (typeof(Type).IsAssignableFrom(conversionType))
+ else if (conversionType.As<Type>())
{
return GetType((String)value, true);
}
@@ -679,10 +676,10 @@ namespace NewLife.Reflection
/// <param name="type"></param>
/// <param name="baseType"></param>
/// <returns></returns>
- public Boolean IsSubOf(Type type, Type baseType)
+ public Boolean As(Type type, Type baseType)
{
if (type == null) return false;
- if (type == baseType) return false;
+ if (type == baseType) return true;
if (baseType.IsAssignableFrom(type)) return true;
diff --git a/NewLife.Core/Reflection/Reflect.cs b/NewLife.Core/Reflection/Reflect.cs
index a653bb8..145d812 100644
--- a/NewLife.Core/Reflection/Reflect.cs
+++ b/NewLife.Core/Reflection/Reflect.cs
@@ -413,25 +413,34 @@ namespace NewLife.Reflection
{
return Type.GetTypeCode(type);
}
+
+ ///// <summary>是否能够转为指定基类</summary>
+ ///// <typeparam name="T"></typeparam>
+ ///// <param name="type"></param>
+ ///// <returns></returns>
+ //public static Boolean As<T>(this Type type)
+ //{
+ // return type != null && typeof(T).IsAssignableFrom(type);
+ //}
#endregion
#region 插件
- /// <summary>是否子类</summary>
+ /// <summary>是否能够转为指定基类</summary>
/// <param name="type"></param>
/// <param name="baseType"></param>
/// <returns></returns>
- public static Boolean IsSubOf(this Type type, Type baseType)
+ public static Boolean As(this Type type, Type baseType)
{
- return Provider.IsSubOf(type, baseType);
+ return Provider.As(type, baseType);
}
- /// <summary>是否子类</summary>
+ /// <summary>是否能够转为指定基类</summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
- public static Boolean IsSubOf<T>(this Type type)
+ public static Boolean As<T>(this Type type)
{
- return Provider.IsSubOf(type, typeof(T));
+ return Provider.As(type, typeof(T));
}
/// <summary>在指定程序集中查找指定基类的子类</summary>
diff --git a/NewLife.Core/Remoting/ApiAction.cs b/NewLife.Core/Remoting/ApiAction.cs
index aedfa10..86d87fc 100644
--- a/NewLife.Core/Remoting/ApiAction.cs
+++ b/NewLife.Core/Remoting/ApiAction.cs
@@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
+using NewLife.Reflection;
namespace NewLife.Remoting
{
@@ -100,7 +101,7 @@ namespace NewLife.Remoting
var type = Method.ReturnType;
var rtype = type.Name;
- if (typeof(Task).IsAssignableFrom(type))
+ if (type.As<Task>())
{
if (type.IsGenericType)
rtype = "Void";
diff --git a/NewLife.Core/Remoting/ApiClient.cs b/NewLife.Core/Remoting/ApiClient.cs
index bbabd8a..f59879a 100644
--- a/NewLife.Core/Remoting/ApiClient.cs
+++ b/NewLife.Core/Remoting/ApiClient.cs
@@ -387,7 +387,9 @@ namespace NewLife.Remoting
/// <returns></returns>
public override Object GetService(Type serviceType)
{
- if (serviceType == GetType()) return this;
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(IApiClient)) return Client;
return base.GetService(serviceType);
diff --git a/NewLife.Core/Remoting/ApiHost.cs b/NewLife.Core/Remoting/ApiHost.cs
index 520c606..fdb5550 100644
--- a/NewLife.Core/Remoting/ApiHost.cs
+++ b/NewLife.Core/Remoting/ApiHost.cs
@@ -5,6 +5,7 @@ using NewLife.Collections;
using NewLife.Data;
using NewLife.Log;
using NewLife.Messaging;
+using NewLife.Reflection;
namespace NewLife.Remoting
{
@@ -192,8 +193,9 @@ namespace NewLife.Remoting
/// <returns></returns>
public virtual Object GetService(Type serviceType)
{
- if (serviceType == GetType()) return this;
- if (serviceType == typeof(ApiHost)) return this;
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(IApiHost)) return this;
if (serviceType == typeof(IApiManager)) return Manager;
if (serviceType == typeof(IEncoder) && Encoder != null) return Encoder;
diff --git a/NewLife.Core/Remoting/ApiHttpServer.cs b/NewLife.Core/Remoting/ApiHttpServer.cs
index d2b5fa2..acc9f35 100644
--- a/NewLife.Core/Remoting/ApiHttpServer.cs
+++ b/NewLife.Core/Remoting/ApiHttpServer.cs
@@ -1,5 +1,6 @@
using System;
using NewLife.Net;
+using NewLife.Reflection;
namespace NewLife.Remoting
{
@@ -32,14 +33,17 @@ namespace NewLife.Remoting
return true;
}
- /// <summary>获取服务提供者</summary>
- /// <param name="serviceType"></param>
- /// <returns></returns>
- public override Object GetService(Type serviceType)
- {
- if (serviceType == typeof(ApiHttpServer)) return Provider;
+ ///// <summary>获取服务提供者</summary>
+ ///// <param name="serviceType"></param>
+ ///// <returns></returns>
+ //public override Object GetService(Type serviceType)
+ //{
+ // // 服务类是否当前类的基类
+ // if (GetType().As(serviceType)) return this;
- return base.GetService(serviceType);
- }
+ // if (serviceType == typeof(ApiHttpServer)) return Provider;
+
+ // return base.GetService(serviceType);
+ //}
}
}
\ No newline at end of file
diff --git a/NewLife.Core/Remoting/ApiNetClient.cs b/NewLife.Core/Remoting/ApiNetClient.cs
index ae38b2f..ffe5a96 100644
--- a/NewLife.Core/Remoting/ApiNetClient.cs
+++ b/NewLife.Core/Remoting/ApiNetClient.cs
@@ -4,6 +4,7 @@ using NewLife.Data;
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Net;
+using NewLife.Reflection;
namespace NewLife.Remoting
{
@@ -55,11 +56,6 @@ namespace NewLife.Remoting
var tc = Client;
tc.MessageReceived += Client_Received;
tc.Log = Log;
-#if DEBUG
- //tc.LogSend = true;
- //tc.LogReceive = true;
- //tc.Timeout = 60 * 1000;
-#endif
tc.Opened += Client_Opened;
return Active = tc.Open();
@@ -117,9 +113,10 @@ namespace NewLife.Remoting
/// <returns></returns>
public Object GetService(Type serviceType)
{
- if (serviceType == GetType()) return this;
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(IApiClient)) return this;
- if (serviceType == typeof(IApiSession)) return this;
if (serviceType == typeof(ISocketClient)) return Client;
return Provider?.GetService(serviceType);
diff --git a/NewLife.Core/Remoting/ApiNetServer.cs b/NewLife.Core/Remoting/ApiNetServer.cs
index 7c36b87..73b73a8 100644
--- a/NewLife.Core/Remoting/ApiNetServer.cs
+++ b/NewLife.Core/Remoting/ApiNetServer.cs
@@ -62,6 +62,9 @@ namespace NewLife.Remoting
/// <returns></returns>
public virtual Object GetService(Type serviceType)
{
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(ApiServer)) return Provider;
if (serviceType == typeof(IEncoder) && Encoder != null) return Encoder;
if (serviceType == typeof(IApiHandler) && Handler != null) return Handler;
@@ -135,6 +138,9 @@ namespace NewLife.Remoting
/// <returns></returns>
public Object GetService(Type serviceType)
{
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(IApiSession)) return this;
if (serviceType == typeof(IApiServer)) return Host;
diff --git a/NewLife.Core/Remoting/ApiServer.cs b/NewLife.Core/Remoting/ApiServer.cs
index 926ffb9..d645679 100644
--- a/NewLife.Core/Remoting/ApiServer.cs
+++ b/NewLife.Core/Remoting/ApiServer.cs
@@ -188,8 +188,9 @@ namespace NewLife.Remoting
/// <returns></returns>
public override Object GetService(Type serviceType)
{
- if (serviceType == GetType()) return this;
- if (serviceType == typeof(ApiServer)) return this;
+ // 服务类是否当前类的基类
+ if (GetType().As(serviceType)) return this;
+
if (serviceType == typeof(IServer)) return this;
return base.GetService(serviceType);
diff --git a/NewLife.Core/Remoting/ApiTest.cs b/NewLife.Core/Remoting/ApiTest.cs
index 400d5ca..b520309 100644
--- a/NewLife.Core/Remoting/ApiTest.cs
+++ b/NewLife.Core/Remoting/ApiTest.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using NewLife.Log;
+using NewLife.Net;
namespace NewLife.Remoting
{
@@ -25,11 +26,17 @@ namespace NewLife.Remoting
var svr = new ApiServer(3344);
svr.Add("http://*:888/");
svr.Log = XTrace.Log;
+ svr.EncoderLog = XTrace.Log;
//svr.Encoder = new JsonEncoder();
//GlobalFilters.Add(new FFAttribute { Name = "全局" });
//GlobalFilters.Add(new FEAttribute { Name = "全局" });
svr.Register<ApiSession>();
svr.Register<HelloController>();
+
+ var ns = svr.Servers[0].GetService<NetServer>();
+ ns.LogSend = true;
+ ns.LogReceive = true;
+
svr.Start();
Console.ReadKey();
@@ -37,14 +44,20 @@ namespace NewLife.Remoting
private static async void TestClient()
{
- //var client = new ApiClient("tcp://127.0.0.1:3344");
+ var client = new ApiClient("tcp://127.0.0.1:3344");
//var client = new ApiClient("udp://127.0.0.1:3344");
- var client = new ApiClient("http://127.0.0.1:888");
+ //var client = new ApiClient("http://127.0.0.1:888");
//var client = new ApiClient("ws://127.0.0.1:888");
client.Log = XTrace.Log;
+ client.EncoderLog = XTrace.Log;
//client.Encoder = new JsonEncoder();
client.UserName = "Stone";
client.Password = "Stone";
+
+ var sc = client.Client.GetService<ISocketClient>();
+ sc.LogSend = true;
+ sc.LogReceive = true;
+
client.Open();
var logined = await client.LoginAsync();
diff --git a/NewLife.Core/Serialization/Binary/BinaryComposite.cs b/NewLife.Core/Serialization/Binary/BinaryComposite.cs
index bcabb32..a3b3cca 100644
--- a/NewLife.Core/Serialization/Binary/BinaryComposite.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryComposite.cs
@@ -152,8 +152,7 @@ namespace NewLife.Serialization
// 不支持基本类型
if (Type.GetTypeCode(type) != TypeCode.Object) return false;
// 不支持基类不是Object的特殊类型
- //if (type.BaseType != typeof(Object)) return false;
- if (!typeof(Object).IsAssignableFrom(type)) return false;
+ if (!type.As<Object>()) return false;
var ims = Host.IgnoreMembers;
diff --git a/NewLife.Core/Serialization/Binary/BinaryDictionary.cs b/NewLife.Core/Serialization/Binary/BinaryDictionary.cs
index 783229a..ab8970e 100644
--- a/NewLife.Core/Serialization/Binary/BinaryDictionary.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryDictionary.cs
@@ -21,7 +21,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean Write(Object value, Type type)
{
- if (!typeof(IDictionary).IsAssignableFrom(type)) return false;
+ if (!type.As<IDictionary>()) return false;
var dic = value as IDictionary;
@@ -50,7 +50,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean TryRead(Type type, ref Object value)
{
- if (!typeof(IDictionary).IsAssignableFrom(type)) return false;
+ if (!type.As<IDictionary>()) return false;
// 子元素类型
var gs = type.GetGenericArguments();
diff --git a/NewLife.Core/Serialization/Binary/BinaryList.cs b/NewLife.Core/Serialization/Binary/BinaryList.cs
index a8a3e15..bd7b384 100644
--- a/NewLife.Core/Serialization/Binary/BinaryList.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryList.cs
@@ -21,7 +21,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean Write(Object value, Type type)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
var list = value as IList;
// 先写入长度
@@ -48,7 +48,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean TryRead(Type type, ref Object value)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
// 先读取长度
var count = Host.ReadSize();
@@ -57,7 +57,7 @@ namespace NewLife.Serialization
if (value == null && type != null)
{
// 数组的创建比较特别
- if (typeof(Array).IsAssignableFrom(type))
+ if (type.As<Array>())
value = Array.CreateInstance(type.GetElementTypeEx(), count);
else
value = type.CreateInstance();
diff --git a/NewLife.Core/Serialization/Binary/BinaryPair.cs b/NewLife.Core/Serialization/Binary/BinaryPair.cs
index 30dd49a..b471f8f 100644
--- a/NewLife.Core/Serialization/Binary/BinaryPair.cs
+++ b/NewLife.Core/Serialization/Binary/BinaryPair.cs
@@ -187,7 +187,7 @@ namespace NewLife.Serialization
#region 字典名值对
private Boolean WriteDictionary(Object value, Type type)
{
- if (!typeof(IDictionary).IsAssignableFrom(type)) return false;
+ if (!type.As<IDictionary>()) return false;
var dic = value as IDictionary;
@@ -205,7 +205,7 @@ namespace NewLife.Serialization
private Boolean TryReadDictionary(Type type, ref Object value)
{
- if (!typeof(IDictionary).IsAssignableFrom(type)) return false;
+ if (!type.As<IDictionary>()) return false;
// 子元素类型
var gs = type.GetGenericArguments();
@@ -239,7 +239,7 @@ namespace NewLife.Serialization
#region 数组名值对
private Boolean WriteArray(Object value, Type type)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
var list = value as IList;
if (list == null || list.Count == 0) return true;
@@ -255,7 +255,7 @@ namespace NewLife.Serialization
private Boolean TryReadArray(Type type, ref Object value)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
// 子元素类型
var elmType = type.GetElementTypeEx();
@@ -324,7 +324,7 @@ namespace NewLife.Serialization
if (Type.GetTypeCode(type) != TypeCode.Object) return false;
// 不支持基类不是Object的特殊类型
//if (type.BaseType != typeof(Object)) return false;
- if (!typeof(Object).IsAssignableFrom(type)) return false;
+ if (!type.As<Object>()) return false;
var ims = Host.IgnoreMembers;
diff --git a/NewLife.Core/Serialization/Json/JsonArray.cs b/NewLife.Core/Serialization/Json/JsonArray.cs
index 0102ae5..5ea8b97 100644
--- a/NewLife.Core/Serialization/Json/JsonArray.cs
+++ b/NewLife.Core/Serialization/Json/JsonArray.cs
@@ -68,7 +68,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean Write(Object value, Type type)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
var list = value as IList;
@@ -92,7 +92,7 @@ namespace NewLife.Serialization
/// <returns></returns>
public override Boolean TryRead(Type type, ref Object value)
{
- if (!typeof(IList).IsAssignableFrom(type)) return false;
+ if (!type.As<IList>()) return false;
// 先读取
if (!Host.Read("[")) return false;
@@ -110,7 +110,7 @@ namespace NewLife.Serialization
}
// 数组的创建比较特别
- if (typeof(Array).IsAssignableFrom(type))
+ if (type.As<Array>())
{
value = Array.CreateInstance(type.GetElementTypeEx(), list.Count);
list.CopyTo((Array)value, 0);
diff --git a/NewLife.Core/Serialization/Json/JsonComposite.cs b/NewLife.Core/Serialization/Json/JsonComposite.cs
index e7f4f42..c9589f8 100644
--- a/NewLife.Core/Serialization/Json/JsonComposite.cs
+++ b/NewLife.Core/Serialization/Json/JsonComposite.cs
@@ -123,7 +123,7 @@ namespace NewLife.Serialization
if (Type.GetTypeCode(type) != TypeCode.Object) return false;
// 不支持基类不是Object的特殊类型
//if (type.BaseType != typeof(Object)) return false;
- if (!typeof(Object).IsAssignableFrom(type)) return false;
+ if (!type.As<Object>()) return false;
var ms = GetMembers(type);
WriteLog("JsonRead类{0} 共有成员{1}个", type.Name, ms.Count);
diff --git a/NewLife.Core/Serialization/Json/JsonReader.cs b/NewLife.Core/Serialization/Json/JsonReader.cs
index db2f34c..34b43cc 100644
--- a/NewLife.Core/Serialization/Json/JsonReader.cs
+++ b/NewLife.Core/Serialization/Json/JsonReader.cs
@@ -218,11 +218,11 @@ namespace NewLife.Serialization
val = Convert.FromBase64String((String)v);
else if (pt.IsArray)
val = CreateArray((IList<Object>)v, pt, pt.GetElementTypeEx());
- else if (typeof(IList).IsAssignableFrom(pt))
+ else if (pt.As<IList>())
val = CreateGenericList((IList<Object>)v, pt, pt.GetElementTypeEx());
else if (pt.IsGenericType && typeof(Dictionary<,>).IsAssignableFrom(pt.GetGenericTypeDefinition()))
val = CreateStringKeyDictionary(vdic, pt, pt.GetGenericArguments());
- else if (typeof(IDictionary).IsAssignableFrom(pt))
+ else if (pt.As<IDictionary>())
val = CreateDictionary((IList<Object>)v, pt, pt.GetGenericArguments());
else if (pt == typeof(NameValueCollection))
val = CreateNV(vdic);
diff --git a/NewLife.Core/Serialization/Xml/XmlComposite.cs b/NewLife.Core/Serialization/Xml/XmlComposite.cs
index acc6cd6..3da58b1 100644
--- a/NewLife.Core/Serialization/Xml/XmlComposite.cs
+++ b/NewLife.Core/Serialization/Xml/XmlComposite.cs
@@ -71,7 +71,7 @@ namespace NewLife.Serialization
if (Type.GetTypeCode(type) != TypeCode.Object) return false;
// 不支持基类不是Object的特殊类型
//if (type.BaseType != typeof(Object)) return false;
- if (!typeof(Object).IsAssignableFrom(type)) return false;
+ if (!type.As<Object>()) return false;
var reader = Host.GetReader();
diff --git a/NewLife.Core/Xml/XmlHelper.cs b/NewLife.Core/Xml/XmlHelper.cs
index 8a01dba..f51dee4 100644
--- a/NewLife.Core/Xml/XmlHelper.cs
+++ b/NewLife.Core/Xml/XmlHelper.cs
@@ -329,7 +329,7 @@ namespace NewLife.Xml
Type elmType = null;
if (type.HasElementType)
elmType = type.GetElementType();
- else if (typeof(IEnumerable).IsAssignableFrom(type) && type.IsGenericType && type.GetGenericArguments().Length == 1)
+ else if (type.IsGenericType && type.GetGenericArguments().Length == 1 && type.As<IEnumerable>())
elmType = type.GetGenericArguments()[0];
if (elmType != null && elmType.Name.EqualIgnoreCase(node.ChildNodes[0].Name))
diff --git a/NewLife.Cube/Common/ControllerBaseX.cs b/NewLife.Cube/Common/ControllerBaseX.cs
index 87b6386..85ff90e 100644
--- a/NewLife.Cube/Common/ControllerBaseX.cs
+++ b/NewLife.Cube/Common/ControllerBaseX.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Reflection;
using System.Web.Mvc;
using NewLife.Log;
+using NewLife.Reflection;
using XCode.Membership;
namespace NewLife.Cube
@@ -24,7 +25,7 @@ namespace NewLife.Cube
{
if (method.IsStatic || !method.IsPublic) continue;
- if (!typeof(ActionResult).IsAssignableFrom(method.ReturnType)) continue;
+ if (!method.ReturnType.As<ActionResult>()) continue;
if (method.GetCustomAttribute<HttpPostAttribute>() != null) continue;
if (method.GetCustomAttribute<AllowAnonymousAttribute>() != null) continue;
diff --git a/NewLife.Cube/Common/EntityModelBinder.cs b/NewLife.Cube/Common/EntityModelBinder.cs
index b437014..92fe206 100644
--- a/NewLife.Cube/Common/EntityModelBinder.cs
+++ b/NewLife.Cube/Common/EntityModelBinder.cs
@@ -2,6 +2,7 @@
using System.Linq;
using System.Web.Mvc;
using NewLife.Log;
+using NewLife.Reflection;
using XCode;
namespace NewLife.Cube
@@ -16,7 +17,7 @@ namespace NewLife.Cube
/// <returns></returns>
protected override Object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
- if (typeof(IEntity).IsAssignableFrom(modelType))
+ if (modelType.As<IEntity>())
{
var fact = EntityFactory.CreateOperate(modelType);
if (fact != null)
@@ -54,7 +55,7 @@ namespace NewLife.Cube
/// <returns></returns>
public IModelBinder GetBinder(Type modelType)
{
- if (typeof(IEntity).IsAssignableFrom(modelType)) return new EntityModelBinder();
+ if (modelType.As<IEntity>()) return new EntityModelBinder();
return null;
}
diff --git a/NewLife.Cube/Precompiled/PrecompiledMvcEngine.cs b/NewLife.Cube/Precompiled/PrecompiledMvcEngine.cs
index 2d0235e..7d08916 100644
--- a/NewLife.Cube/Precompiled/PrecompiledMvcEngine.cs
+++ b/NewLife.Cube/Precompiled/PrecompiledMvcEngine.cs
@@ -8,6 +8,7 @@ using System.Web.Compilation;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.WebPages;
+using NewLife.Reflection;
namespace NewLife.Cube.Precompiled
{
@@ -48,45 +49,39 @@ namespace NewLife.Cube.Precompiled
_assemblyLastWriteTime = new Lazy<DateTime>(() => assembly.GetLastWriteTimeUtc(DateTime.MaxValue));
_baseVirtualPath = NormalizeBaseVirtualPath(baseVirtualPath);
AreaViewLocationFormats = new String[]
- {
- "~/Areas/{2}/Views/{1}/{0}.cshtml",
- "~/Areas/{2}/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Areas/{2}/Views/{1}/{0}.cshtml",
+ "~/Areas/{2}/Views/Shared/{0}.cshtml"
+ };
AreaMasterLocationFormats = new String[]
- {
- "~/Areas/{2}/Views/{1}/{0}.cshtml",
- "~/Areas/{2}/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Areas/{2}/Views/{1}/{0}.cshtml",
+ "~/Areas/{2}/Views/Shared/{0}.cshtml"
+ };
AreaPartialViewLocationFormats = new String[]
- {
- "~/Areas/{2}/Views/{1}/{0}.cshtml",
- "~/Areas/{2}/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Areas/{2}/Views/{1}/{0}.cshtml",
+ "~/Areas/{2}/Views/Shared/{0}.cshtml"
+ };
ViewLocationFormats = new String[]
- {
- "~/Views/{1}/{0}.cshtml",
- "~/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Views/{1}/{0}.cshtml",
+ "~/Views/Shared/{0}.cshtml"
+ };
MasterLocationFormats = new String[]
- {
- "~/Views/{1}/{0}.cshtml",
- "~/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Views/{1}/{0}.cshtml",
+ "~/Views/Shared/{0}.cshtml"
+ };
PartialViewLocationFormats = new String[]
- {
- "~/Views/{1}/{0}.cshtml",
- "~/Views/Shared/{0}.cshtml"
- };
+ {
+ "~/Views/{1}/{0}.cshtml",
+ "~/Views/Shared/{0}.cshtml"
+ };
FileExtensions = new String[]
- {
- "cshtml"
- };
- //_mappings = (
- // from type in assembly.GetTypes()
- // where typeof(WebPageRenderingBase).IsAssignableFrom(type)
- // let pageVirtualPath = type.GetCustomAttributes(false).OfType<PageVirtualPathAttribute>().FirstOrDefault()
- // where pageVirtualPath != null
- // select new KeyValuePair<String, Type>(CombineVirtualPaths(_baseVirtualPath, pageVirtualPath.VirtualPath), type)).ToDictionary(t => t.Key, t => t.Value, StringComparer.OrdinalIgnoreCase);
+ {
+ "cshtml"
+ };
_mappings = GetTypeMappings(assembly, _baseVirtualPath);
ViewLocationCache = new PrecompiledViewLocationCache(assembly.FullName, ViewLocationCache);
//_viewPageActivator = (viewPageActivator ?? (DependencyResolver.Current.GetService<IViewPageActivator>() ?? DefaultViewPageActivator.Current));
@@ -229,7 +224,7 @@ namespace NewLife.Cube.Precompiled
{
return (
from type in asm.GetTypes()
- where typeof(WebPageRenderingBase).IsAssignableFrom(type)
+ where type.As<WebPageRenderingBase>()
let pageVirtualPath = type.GetCustomAttributes(false).OfType<PageVirtualPathAttribute>().FirstOrDefault()
where pageVirtualPath != null
select new KeyValuePair<String, Type>(CombineVirtualPaths(baseVirtualPath, pageVirtualPath.VirtualPath), type)).ToDictionary(t => t.Key, t => t.Value, StringComparer.OrdinalIgnoreCase);
diff --git a/XCode/Attributes/MapAttribute.cs b/XCode/Attributes/MapAttribute.cs
index 71862bd..7e948a9 100644
--- a/XCode/Attributes/MapAttribute.cs
+++ b/XCode/Attributes/MapAttribute.cs
@@ -37,7 +37,7 @@ namespace XCode
Name = name;
// 区分实体类和提供者
- if (typeof(MapProvider).IsAssignableFrom(type))
+ if (type.As<MapProvider>())
Provider = Activator.CreateInstance(type) as MapProvider;
else
{
diff --git a/XCode/DataAccessLayer/Model/ModelHelper.cs b/XCode/DataAccessLayer/Model/ModelHelper.cs
index 90d7655..e9c887c 100644
--- a/XCode/DataAccessLayer/Model/ModelHelper.cs
+++ b/XCode/DataAccessLayer/Model/ModelHelper.cs
@@ -218,7 +218,7 @@ namespace XCode.DataAccessLayer
settings.IgnoreComments = true;
var reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(xml)), settings);
- while (reader.NodeType != XmlNodeType.Element) { if (!reader.Read())return null; }
+ while (reader.NodeType != XmlNodeType.Element) { if (!reader.Read()) return null; }
reader.ReadStartElement();
var list = new List<IDataTable>();
@@ -246,7 +246,7 @@ namespace XCode.DataAccessLayer
reader.ReadEndElement();
}
else
- {
+ {
// 这里必须处理,否则加载特殊Xml文件时将会导致死循环
reader.Read();
}
@@ -535,7 +535,8 @@ namespace XCode.DataAccessLayer
}
else if (code == TypeCode.Object)
{
- if (pi.PropertyType.IsArray || typeof(IEnumerable).IsAssignableFrom(pi.PropertyType) || obj is IEnumerable)
+ var ptype = pi.PropertyType;
+ if (ptype.IsArray || ptype.As<IEnumerable>() || obj is IEnumerable)
{
var sb = new StringBuilder();
var arr = obj as IEnumerable;
diff --git a/XCode/Entity/EntityFactory.cs b/XCode/Entity/EntityFactory.cs
index 4eda988..85fc010 100644
--- a/XCode/Entity/EntityFactory.cs
+++ b/XCode/Entity/EntityFactory.cs
@@ -228,7 +228,7 @@ namespace XCode
private static Type GetTypeInternal(String typeName)
{
var type = typeName.GetTypeEx(true);
- if (type != null && typeof(IEntity).IsAssignableFrom(type)) return type;
+ if (type != null && type.As<IEntity>()) return type;
type = null;
var entities = LoadEntities();
diff --git a/XCode/Entity/EntityList.cs b/XCode/Entity/EntityList.cs
index 1ba3eb9..0347709 100644
--- a/XCode/Entity/EntityList.cs
+++ b/XCode/Entity/EntityList.cs
@@ -519,7 +519,7 @@ namespace XCode
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
var type = GetItemType(name);
if (type == null) throw new ArgumentNullException("name", "无法找到字段" + name + "的类型");
- if (!typeof(IComparable).IsAssignableFrom(type)) throw new NotSupportedException(String.Format("排序字段{0}的类型{1}不支持比较!", name, type.FullName));
+ if (!type.As<IComparable>()) throw new NotSupportedException(String.Format("排序字段{0}的类型{1}不支持比较!", name, type.FullName));
var n = 1;
if (isDesc) n = -1;
@@ -554,7 +554,7 @@ namespace XCode
var isDesc = isDescs[i];
var type = GetItemType(name);
- if (!typeof(IComparable).IsAssignableFrom(type)) throw new NotSupportedException("不支持比较!");
+ if (!type.As<IComparable>()) throw new NotSupportedException("不支持比较!");
}
Sort((item1, item2) =>
diff --git a/XTemplate/Templating/Template.cs b/XTemplate/Templating/Template.cs
index 17dd4d6..32ca235 100644
--- a/XTemplate/Templating/Template.cs
+++ b/XTemplate/Templating/Template.cs
@@ -1067,7 +1067,7 @@ namespace XTemplate.Templating
var n = 0;
foreach (var type in ts)
{
- if (!typeof(TemplateBase).IsAssignableFrom(type)) continue;
+ if (!type.As<TemplateBase>()) continue;
className = type.FullName;
if (n++ > 1) break;