HOW TO:使用 UML API 瀏覽關聯性
在 Visual Studio Ultimate 中,模型會包含由不同類型的關聯性連結在一起的項目。此主題將說明如何巡覽程式碼中的模型。
巡覽關聯性
任何關聯性
使用 GetRelatedElements<T>() 尋找所有與指定項目連接的項目。將 T 設為 IRelationship 以巡覽所有類型的關聯性,或是使用更明確的型別 (例如 IAssociation) 僅巡覽該型別。
IElement anElement;
// Select all elements related to anElement.
Context.CurrentDiagram.SelectShapes (
anElement.GetRelatedElements<IRelationship>()
.SelectMany(e=>e.Shapes()).ToArray());
使用 GetRelatedLinks<T>() 尋找所有與項目連接的關聯性。
// Process all relationships connected to an element.
foreach (IRelationship relationship in
anElement.GetRelatedLinks<IRelationship>())
{
Debug.Assert(relationship.SourceElement == anElement
|| relationship.TargetElement == anElement);
}
關聯
關聯是兩個屬性之間的關聯性,這兩個屬性各屬於一個 Classifier。
IClassifier classifier; // class, interface, component, actor, ...
// Get all the associations sourced from this classifier
foreach (IProperty p in classifier.GetOutgoingAssociationEnds())
{
// p represents the end further end of an association.
IType oppositeElement = p.Type;
// The type to which this association connects classifier
IProperty oppositeProperty = p.Opposite;
// The nearer end of the association.
Debug.Assert(oppositeProperty.Type == classifier);
IAssociation association = p.Association;
Debug.Assert(association.MemberEnds.Contains(p)
&& association.MemberEnds.Contains(oppositeProperty));
}
Generalization 和 Realization
存取 Generalization 的另一端:
foreach (IClassifier supertype in classifier.Generals) {…}
foreach (IClassifier subtype in classifier.GetSpecifics()) {…}
Access the relationship itself:
foreach (IGeneralization gen in classifier.Generalizations)
{ Debug.Assert(classifier == gen.Specific); }
/// InterfaceRealization:
IEnumerable<IInterface> GetRealizedInterfaces
(this IBehavioredClassifier classifier);
IEnumerable<IBehavioredClassifier> GetRealizingClassifiers
(this IInterface interface);
相依性
/// Returns the elements depending on this element
IEnumerable<INamedElement> GetDependencyClients(this INamedElement element);
/// Returns the elements this element depends on
IEnumerable<INamedElement> INamedElement GetDependencySuppliers(this INamedElement element);
Activity Edge
/// Returns the nodes targeted by edges outgoing from this one
IEnumerable<IActivityNode> GetActivityEdgeTargets(this IActivityNode node);
/// Returns the nodes sourcing edges incoming to this one
IEnumerable<IActivityNode> GetActivityEdgeSources(this IActivityNode node);
連接器 (組件和委派)
/// Returns the elements connected via assembly
/// or delegation to this one
IEnumerable<IConnectableElement> GetConnectedElements(this IConnectableElement element);
訊息和生命線
IEnumerable<IMessage> GetAllOutgoingMessages(this ILifeline lifeline);
// both from lifeline and execution occurrences
IEnumerable<IMessage> GetAllIncomingMessages(this ILifeline lifeline);
ILifeline GetSourceLifeline(this IMessage message);
// may return null for found messages
ILifeline GetTargetLifeline(this IMessage message);
// may return null for lost messages
封裝匯入
IEnumerable<IPackage>GetImportedPackages(this INamespace namespace);
IEnumerable<INamespace> GetImportingNamespaces(this IPackage package);
使用案例擴充和包含
IEnumerable<IUseCase>GetExtendedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetExtendingCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludingCases(this IUseCase usecase);
列舉關聯性
所有會傳回多個值的 UML 模型屬性,均符合 IEnumerable<> 介面。這表示您可以使用 LINQ 查詢運算式和 System.Linq 命名空間中定義的擴充方法。
例如:
from shape in Context.CurrentDiagram.GetSelectedShapes<IClassifier>()
where shape.Color == System.Drawing.Color.Red
select shape.Element