2017-04-18 42 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; 
}