2017-04-18 43 views

回答

0

我已經想通了。

查看例如「foreach(typeMapper.GetItemsToGenerate(itemCollection))中的var實體」中的實體變量,這是一個包含MetadataProperties的GlobalItem(https://msdn.microsoft.com/en-us/library/system.data.metadata.edm.globalitem(v=vs.110).aspx)。

使用簡單的foreach

foreach(var mp in entity.MetadataProperties) 
{ 
    this.WriteLine("{0} = '{1}'", mp.Name, mp.Value); 
} 

結果列表中的

Name = 'Role' 
NamespaceName = 'Model1' 
Abstract = 'False' 
... 
http://saop.si:RecordTracked = '<a:RecordTracked xmlns:a="http://saop.si">true</a:RecordTracked>' 
http://saop.si:DisplayMember = '<a:DisplayMember xmlns:a="http://saop.si">true</a:DisplayMember>' 

,你可以看到,自定義屬性(RecordTracked,顯示名稱)也列出了清單這些屬性。

我已經在公共類CodeStringGenerator內創建了2個函數來檢索任何自定義屬性。這樣稱呼:

CodeStringGenerator.GetCustomPropertyAsBoolean(entity, "RecordTracked"); 


private bool GetCustomPropertyAsBoolean(GlobalItem item, string propertyName) 
{ 
    var _value = GetCustomProperty(item, propertyName); 
    if (string.IsNullOrEmpty(_value)) 
    { return false; } 

    return _value.Equals("true", StringComparison.CurrentCultureIgnoreCase);   
} 

private string GetCustomProperty(GlobalItem item, string propertyName) 
{ 
    var _found = item.MetadataProperties.FirstOrDefault(p => p.Name.StartsWith("http://") && 
                  p.Name.EndsWith(propertyName, StringComparison.CurrentCultureIgnoreCase)); 
    if (_found == null) 
    { return string.Empty; } 

    var _value = _found.Value as System.Xml.Linq.XElement; 
    if (_value == null) 
    { return string.Empty; } 

    return _value.Value; 
}