Click here to Skip to main content
14,837,675 members
Articles / Programming Languages / C#
Posted 11 Jul 2007


47 bookmarked

Simple Ontology Support for C#

Rate me:
Please Sign up or sign in to vote.
4.10/5 (10 votes)
11 Jul 20073 min read
Extending the class hierarchy with semantic information


The class hierarchy of an object-oriented programming language represents a static ontology. It defines a terminology for entities, their attributes and relations. Problems arise when an application needs different classifications at runtime. This may lead to ambiguities because of multiple inheritance or hacks that are needed to implement this feature.

This article describes an approach that extends the existing class information by additional ontologies to provide rich and dynamic semantic information. A case study is presented, that shows the flexibility and added value of the concept and how it can be used. In addition, this method integrates well with the object-oriented design process.


Gruber (1993) defines an Ontology as 'an explicit specification of a conceptualization', i.e. a view onto a system is described by a standardized terminology and relations between entities. In many cases, there is no single ontology but many different and concurrent ones (cmp. Hesse (2002)).

A class hierarchy of an object-oriented programming language, e.g. C#, defines a static Ontology; it describes a view onto a system in terms of objects, attributes and relationships. The following figure shows an example:

A sample class hierarchy.

The example is taken from an implementation of a bond graph. A bond graph consists of different types of nodes and bonds between them. The problem here is, that different algorithms require a special view onto the nodes, e.g. sometimes the nodes are classified by "type" and sometimes by their number of "ports". The SE and SF nodes, for example, can be classified as: by-type as sources and by-port as single ports.

Due to the lack of support of multiple inheritance in C#, it is not possible to implement this concurrent classification. Interfaces are a kind of solution but this would lose much valuable information.

The idea is to extend the class hierarchy by ontologies and to cross-reference the entities:

Ontology and Class Hierarchy.

Depending on the used ontology, a SE node is either a Source or a SinglePort. The advantage is that the ambiguity of the father-child relationship does not lead to a conflict and a single (clean) class hierarchy is maintained.

Using the code

Ontologies are implemented through the E56.Ontology library, which provides classes for semantic annotations of existing C# classes. SemanticType is the counterpart of System.Type and the base for the extension. A semantic type is virtual if no C# class with the same name exists, otherwise this class is cross-referenced. SemanticObject extends System.Object. To use a class in an ontology, it must be derived from SemanticObject.

An Example:

class Element : SemanticObject { ... }
class Node : Element { ... }
class SE : Node { ... }

Different Ontologies can be loaded through:


Now, we can use the additional information:

// Create an instance of a class
SF sf = new SF();
// Zugriff auf dessen semantischen Typs
SemanticType root = sf.SemanticType;
// Using the schema 'by-port' sf is a...
SemanticType st1 = sf.IsA("by-port");
Console.WriteLine("'by-port': {0} is a {1}", root, st1);
// Using the schema 'by-typet' sf is a...
SemanticType st2 = sf.IsA("by-type");
Console.WriteLine("'by-type': {0} is a {1}", root, st2);

Through the SemanticType property, semantic type information can be accessed. This is implemented as a lookup of the C# type within the Ontology. The method IsA(string schema) traverses the hierarchy and returns the father object. The result is:

'by-port': [SF/BondGraphs.Core.SF] is a [SinglePort/virtual]
'by-type': [SF/BondGraphs.Core.SF] is a [Source/virtual]

Technical information

The implementation is based on the System.Reflection API, that allows access to classes, methods, and even variables at runtime. The ontology itself is described in a simple XML format, which will be replaced by OWL. The Web Ontology Language (OWL) is a W3C standard for specifying semantic information in a machine readable way.

The by-type schema looks like this:

<?xml version="1.0" ?>
<schema namespace="example">
  <object name="Element" />
  <object name="Node" isA="Element" />
  <object name="Source" isA="Node" />
  <object name="SF" isA="Source" />
  <object name="SE" isA="Source" />


  • Gruber, T. R. (1993). A Translation Approach to Portable Ontology Specifications. Knowledge Acquisition, 5(2):199–220.
  • Hesse, W. (2002). Ontologie(n). Informatik Spektrum, 25(6):477–480.
  • W3C (2004). Web Ontology Language., accessed 11 July 2007.


  • 11 July 2007 - Initial version


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Germany Germany
Utilizing my experience of industry-scale real-time graphics programming, design, development of software, as well as European research projects I want to bring in new ideas for creative projects.
- 2009-now BTC AG: software for the renewable energy sector.
- 2007-2009 Digital Media: Audio, Graphics and GIS Web Services(
- 2001-2007 artecLab://art/work/technology: Mixed Reality, Computer Games and eLearning
- 1998-2001 STN ATLAS Elektronik: real-time graphics for ground warfare simulation

For a complete resume see

Comments and Discussions

QuestionWhat about E56 Pin
Mr.Sourav.Maitra21-Mar-11 10:02
MemberMr.Sourav.Maitra21-Mar-11 10:02 
QuestionPractical usage? Pin
Boudino29-Jul-07 6:55
MemberBoudino29-Jul-07 6:55 
AnswerRe: Practical usage? Pin
mf04029-Jul-07 20:50
Membermf04029-Jul-07 20:50 
GeneralNice Pin
Paul Conrad15-Jul-07 7:57
professionalPaul Conrad15-Jul-07 7:57 
GeneralUsing Interfaces Pin
Alex Espinoza11-Jul-07 9:19
MemberAlex Espinoza11-Jul-07 9:19 
AnswerRe: Using Interfaces Pin
mf04011-Jul-07 21:13
Membermf04011-Jul-07 21:13 
GeneralRe: Using Interfaces Pin
Alex Espinoza12-Jul-07 7:15
MemberAlex Espinoza12-Jul-07 7:15 
AnswerRe: Using Interfaces Pin
mf04012-Jul-07 21:07
Membermf04012-Jul-07 21:07 
GeneralRe: Using Interfaces Pin
Alex Espinoza13-Jul-07 5:56
MemberAlex Espinoza13-Jul-07 5:56 
GeneralRe: Using Interfaces Pin
mf04016-Jul-07 2:00
Membermf04016-Jul-07 2:00 
GeneralRe: Using Interfaces Pin
Alex Espinoza16-Jul-07 4:37
MemberAlex Espinoza16-Jul-07 4:37 
GeneralCoupling question Pin
sadavoya11-Jul-07 2:55
Membersadavoya11-Jul-07 2:55 
AnswerRe: Coupling question Pin
mf04011-Jul-07 21:20
Membermf04011-Jul-07 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.