I'm using the following code to determine the level name of an element. The code is encountering a System.AccessViolationException when I use the LevelHandle.Name to retrieve the level name. Works fine on some elements, but throws this exception on others. The code doesn't actually catch the exception due to the unmanaged nature of the application throwing the exception (CONNECT) but if I watch the code blow up in the debugger the evaluation of the levelname shows the exception.
///<summary>Gets the level of an Element.</summary>
///<param name="element">The source element from which to retrieve the level name.</param>
///<returns>The level name that the element is on.</returns>
publicstaticstring GetElementLevelName(Element element)
{
// Get Element Property Getter
ElementPropertiesGetter elementPropertiesGetter = newElementPropertiesGetter(element);
// Get DGN Model element belongs to
DgnModel dgnModel = element.DgnModel;
// Get DGN File
DgnFile dgnFile = dgnModel.GetDgnFile();
// Get File Level Cache
FileLevelCache fileLevelCache = dgnFile.GetLevelCache();
// Get Level Id
LevelId levelId = elementPropertiesGetter.Level;
// Get Level Handle
LevelHandle levelHandle = fileLevelCache.GetLevel(levelId, true);
// Return Level Name
string levelName;
try
{
levelName = levelHandle.Name;
}
catch (System.AccessViolationException)
{
levelName = "";
throw;
}
return levelName;
}