WCF:我必须为哪种 属性 类型调用 SetLink
WCF: For which property type do I have to call SetLink
我想做一个非常通用的 class 来将对象保存到 WCF 服务。我得到任何类型的对象并通过反射分析对象属性。将对象保存到服务上下文中时,我需要知道是否必须为 属性 调用 DataServiceContext
的 SetLink(...)
方法。
因此我需要一种方法来确定是否必须调用 SetLink(...)
。我已经尝试过自己做:
private bool IsLinkedProperty(PropertyInfo propertyInfo)
{
return (propertyInfo != null) && propertyInfo.PropertyType.IsClass;
}
但是这个函数不适用于字符串属性,也许对其他属性也不起作用。有人有合适的功能吗?
我最终得到了以下解决方案。我在 qestion of Nathan Ridley:
中找到了提示
/// <summary>
/// Helper class for analyzing a type.
/// </summary>
public static class TypeAnalyzer
{
/// <summary>
/// Calculates if the given type is a "simple" type.
/// </summary>
/// <param name="type">Type to be checked for simplicity.</param>
/// <returns>True, if the type is "simple";false otherwise.</returns>
/// <remarks>
/// The following types are assumed to be simple:
/// <list type="*">
/// <item>string</item>
/// <item>int</item>
/// <item>decimal</item>
/// <item>float</item>
/// <item><see cref="StringComparison"/> (enum type)</item>
/// <item>int? (nullable simple types)</item>
/// </list>
/// The following types are not simple:
/// <list type="*">
/// <item>Point (struct)</item>
/// <item>Point? (nullable struct)</item>
/// <item>StringBuilder (class)</item>
/// </list>
/// </remarks>
public static bool IsSimple(this Type type)
{
if (IsNullableType(type))
return IsNestedTypeSimple(type);
return type.IsPrimitive
|| type.IsEnum
|| type.Equals(typeof(string))
|| type.Equals(typeof(decimal))
|| type.Equals(typeof(DateTime))
|| type.Equals(typeof(Guid));
}
private static bool IsNestedTypeSimple(Type type)
{
var nestedType = Nullable.GetUnderlyingType(type);
return IsSimple(nestedType);
}
private static bool IsNullableType(Type type)
{
return Nullable.GetUnderlyingType(type) != null;
}
}
用 NUnit 编写的测试用例是:
[TestFixture]
public class TypeAnalyzerTests
{
[TestCase(typeof(string), true)]
[TestCase(typeof(int), true)]
[TestCase(typeof(decimal), true)]
[TestCase(typeof(float), true)]
[TestCase(typeof(StringComparison), true)]
[TestCase(typeof(int?), true)]
[TestCase(typeof(decimal?), true)]
[TestCase(typeof(StringComparison?), true)]
[TestCase(typeof(object), false)]
[TestCase(typeof(Point), false)]
[TestCase(typeof(Point?), false)]
[TestCase(typeof(StringBuilder), false)]
[TestCase(typeof(DateTime), true)]
[TestCase(typeof(Guid), true)]
[TestCase(typeof(Guid?), true)]
public void IsSimple_WhenCalledForType_ReturnsExpectedResult(Type type, bool expectedResult)
{
var isSimple = TypeAnalyzer.IsSimple(type);
Assert.That(isSimple, Is.EqualTo(expectedResult));
}
}
最后我把问题中提到的方法改成了:
private bool IsLinkedProperty()
{
return (_propertyInfo != null) && !_propertyInfo.PropertyType.IsSimple();
}
我想做一个非常通用的 class 来将对象保存到 WCF 服务。我得到任何类型的对象并通过反射分析对象属性。将对象保存到服务上下文中时,我需要知道是否必须为 属性 调用 DataServiceContext
的 SetLink(...)
方法。
因此我需要一种方法来确定是否必须调用 SetLink(...)
。我已经尝试过自己做:
private bool IsLinkedProperty(PropertyInfo propertyInfo)
{
return (propertyInfo != null) && propertyInfo.PropertyType.IsClass;
}
但是这个函数不适用于字符串属性,也许对其他属性也不起作用。有人有合适的功能吗?
我最终得到了以下解决方案。我在 qestion of Nathan Ridley:
中找到了提示/// <summary>
/// Helper class for analyzing a type.
/// </summary>
public static class TypeAnalyzer
{
/// <summary>
/// Calculates if the given type is a "simple" type.
/// </summary>
/// <param name="type">Type to be checked for simplicity.</param>
/// <returns>True, if the type is "simple";false otherwise.</returns>
/// <remarks>
/// The following types are assumed to be simple:
/// <list type="*">
/// <item>string</item>
/// <item>int</item>
/// <item>decimal</item>
/// <item>float</item>
/// <item><see cref="StringComparison"/> (enum type)</item>
/// <item>int? (nullable simple types)</item>
/// </list>
/// The following types are not simple:
/// <list type="*">
/// <item>Point (struct)</item>
/// <item>Point? (nullable struct)</item>
/// <item>StringBuilder (class)</item>
/// </list>
/// </remarks>
public static bool IsSimple(this Type type)
{
if (IsNullableType(type))
return IsNestedTypeSimple(type);
return type.IsPrimitive
|| type.IsEnum
|| type.Equals(typeof(string))
|| type.Equals(typeof(decimal))
|| type.Equals(typeof(DateTime))
|| type.Equals(typeof(Guid));
}
private static bool IsNestedTypeSimple(Type type)
{
var nestedType = Nullable.GetUnderlyingType(type);
return IsSimple(nestedType);
}
private static bool IsNullableType(Type type)
{
return Nullable.GetUnderlyingType(type) != null;
}
}
用 NUnit 编写的测试用例是:
[TestFixture]
public class TypeAnalyzerTests
{
[TestCase(typeof(string), true)]
[TestCase(typeof(int), true)]
[TestCase(typeof(decimal), true)]
[TestCase(typeof(float), true)]
[TestCase(typeof(StringComparison), true)]
[TestCase(typeof(int?), true)]
[TestCase(typeof(decimal?), true)]
[TestCase(typeof(StringComparison?), true)]
[TestCase(typeof(object), false)]
[TestCase(typeof(Point), false)]
[TestCase(typeof(Point?), false)]
[TestCase(typeof(StringBuilder), false)]
[TestCase(typeof(DateTime), true)]
[TestCase(typeof(Guid), true)]
[TestCase(typeof(Guid?), true)]
public void IsSimple_WhenCalledForType_ReturnsExpectedResult(Type type, bool expectedResult)
{
var isSimple = TypeAnalyzer.IsSimple(type);
Assert.That(isSimple, Is.EqualTo(expectedResult));
}
}
最后我把问题中提到的方法改成了:
private bool IsLinkedProperty()
{
return (_propertyInfo != null) && !_propertyInfo.PropertyType.IsSimple();
}