2016-03-23 66 views
0

我想製作一個真正通用的類來將對象保存到WCF服務。我得到任何類型的對象並通過反射分析對象的屬性。當將對象保存到服務上下文中時,我需要知道,是否必須針對屬性調用SetLink(...)方法的DataServiceContextWCF:我必須調用哪種屬性類型SetLink

因此,我需要一個方法來找出,如果我不得不打電話SetLink(...)或不。我已經試圖自己做:

private bool IsLinkedProperty(PropertyInfo propertyInfo) 
{ 
    return (propertyInfo != null) && propertyInfo.PropertyType.IsClass; 
} 

但是這個函數不適用於字符串屬性,也可能用於其他人。有沒有人有適合這個的功能?

+0

可以定義確定是否應該調用該方法的標準? – stuartd

+0

你可以在這裏找到'SetLink'method:[link](https://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.setlink%28v=vs.113%29的.aspx)。據我所知,所有「原始」數據類型。 – scher

+0

它在我看來,你將需要添加元數據(即屬性)到你的屬性知道何時調用該方法,以及爲參數傳遞什麼。 – stuartd

回答

0

我結束了以下解決方案。我發現在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(); 
} 
相關問題