2016-03-23 2 views
0

WCFサービスにオブジェクトを保存するための、本当に汎用的なクラスを作成します。私はどんなタイプのオブジェクトをも取得し、リフレクションによってオブジェクトのプロパティを分析します。オブジェクトをサービスコンテキストに保存するときは、プロパティのDataServiceContextSetLink(...)メソッドを呼び出す必要があるかどうかを知る必要があります。WCF:どのプロパティタイプに対してSetLinkを呼び出す必要がありますか

したがって、SetLink(...)に電話する必要があるかどうかを調べる方法が必要です。

しかし、この機能は文字列プロパティや他の機能では機能しません。誰かがこれにふさわしい機能を持っていますか?

+0

あなたが定義することができますメソッドを呼び出す必要があるかどうかを判断するための基準? – stuartd

+0

'SetLink'メソッドはここにあります:[link](https://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.setlink%28v=vs.113%29 .aspx)。私はすべての "原始的な"データ型について理解しています。 – scher

+0

メソッドを呼び出すタイミングを知るためにプロパティにメタデータ(属性など)を追加し、パラメータを渡す必要があるようです。 – stuartd

答えて

0

私は以下の解決策に終わった。

/// <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の中で書かれた

テストケースは次のとおりです:私はqestion of Nathan Ridleyでヒントを見つけ

[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(); 
} 
関連する問題