[MSCE C# C++]Read TypeAdapter applied ECPropertyValue

This example demonstrates how to read TypeAdapter applied ECPropertyValue.
SDK sample path :
Managed:
examples\DgnEC\ECValueWithTypeAdapter\ManagedExample

Helper helper = new Helper();
LineElement lineElement = helper.CreateLineElement(0, 0, 0, 100);
if (null == lineElement)
    return;

string schemaName = "DgnElementSchema";
DgnECManager manager = DgnECManager.Manager;
var option = new FindInstancesScopeOption(DgnECHostType.Element);
option.SearchPublicChildren = true;
FindInstancesScope scope = FindInstancesScope.CreateScope(helper.GetDefaultModel().GetDgnFile(), option);

IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, schemaName, 1, 0, SchemaMatchType.LatestCompatible);
if( null == schema)
    {
    MessageCenter.Instance.ShowErrorMessage("DgnElementSchema not found", null, MessageAlert.None);
    return;
    }

IECClass lineElementClass = schema.GetClass("LineElement");
if (null == lineElementClass)
    {
    MessageCenter.Instance.ShowErrorMessage("LineElement Class pointer not found", null, MessageAlert.None);
    return;
    }

ECQuery query = new ECQuery(lineElementClass);
query.SelectClause.SelectAllProperties = true;
using (DgnECInstanceCollection ecInstances = manager.FindInstances(scope, query))
    {
    string strTotalLength = "TotalLength";
    string strColor = "Color";
    string strLevel = "Level";

    string totalLengthWithTypeAdapter = null;
    string colorValueWithTypeAdapter = null;
    string levelValueWithTypeAdapter = null;

    double totalLengthWithoutTypeAdapter = 0.0;
    int colorValueWithoutTypeAdapter = 0;
    int levelValueWithoutTypeAdapter = 0;

    foreach (IDgnECInstance instance in ecInstances)
        {
        IECPropertyValue totalLengthPropValue = instance.GetPropertyValue(strTotalLength);
        if(null != totalLengthPropValue)
            {
            totalLengthWithoutTypeAdapter = totalLengthPropValue.DoubleValue;
            ITypeDescriptorContext contextTotalLength;
            TypeConverter typeConvtrTotalLength = ECMultiInstancePropertyAdapter.GetTypeConverterForAllTypes(totalLengthPropValue, out contextTotalLength);
            totalLengthWithTypeAdapter = typeConvtrTotalLength.ConvertToString(contextTotalLength, totalLengthPropValue.NativeValue);
            }

        IECPropertyValue colorPropValue = instance.GetPropertyValue(strColor);
        if (null != colorPropValue)
            {
            colorValueWithoutTypeAdapter = colorPropValue.IntValue;

            ITypeDescriptorContext contextColor;
            TypeConverter typeCnvtrColor = ECMultiInstancePropertyAdapter.GetTypeConverterForAllTypes(colorPropValue, out contextColor);
            colorValueWithTypeAdapter = typeCnvtrColor.ConvertToString(contextColor, colorPropValue.NativeValue);
            }

        IECPropertyValue levelPropValue = instance.GetPropertyValue(strLevel);
        if (null != levelPropValue)
            {
            levelValueWithoutTypeAdapter = levelPropValue.IntValue;

            ITypeDescriptorContext contextLevel;
            TypeConverter typeCnvtrLevel = ECMultiInstancePropertyAdapter.GetTypeConverterForAllTypes(levelPropValue, out contextLevel);
            levelValueWithTypeAdapter = typeCnvtrLevel.ConvertToString(contextLevel, levelPropValue.NativeValue);
            }
        }

    string resultWithoutTypeAdapter = string.Format("{0} -> {1} : {2}, {3} : {4}, {5} : {6} \n", "ECValue Without TypeAdapter", 
                                                    strTotalLength, totalLengthWithoutTypeAdapter.ToString(), strColor,
                                                    colorValueWithoutTypeAdapter.ToString(),
                                                    strLevel, levelValueWithoutTypeAdapter.ToString());
    MessageCenter.Instance.ShowInfoMessage(resultWithoutTypeAdapter, null, MessageAlert.None);

    string resultWithTypeAdapter = string.Format("{0} -> {1} : {2}, {3} : {4}, {5} : {6} \n", "ECValue With TypeAdapter" ,
                                                strTotalLength, totalLengthWithTypeAdapter, strColor, colorValueWithTypeAdapter, strLevel, levelValueWithTypeAdapter);
    MessageCenter.Instance.ShowInfoMessage(resultWithTypeAdapter, null, MessageAlert.None);

Native :
examples\DgnEC\ECValueWithTypeAdapter\NativeExample
DgnModelP dgnModelP = ISessionMgr::GetActiveDgnModelP();
ECValueWithTypeAdapterNativeHelper::m_lineEh1 = CreateLine(*dgnModelP, 0.0, 0.0, 100.0, 0.0);
    
FindInstancesScopeOption instanceScopeOption(DgnECHostType::Element);
FindInstancesScopePtr instanceScopePtr = FindInstancesScope::CreateScope(ECValueWithTypeAdapterNativeHelper::m_lineEh1, instanceScopeOption);
    
WString schemaName = L"DgnElementSchema";
SchemaKey schemaKey(schemaName.c_str(), 1, 0);
SchemaInfo schemaInfo(schemaKey, *dgnModelP->GetDgnFileP());

ECN::ECSchemaPtr elementSchemaPtr = DgnECManager::GetManager().LocateSchemaInDgnFile(schemaInfo, SCHEMAMATCHTYPE_Latest, true);
if (elementSchemaPtr.IsNull())
    {
    wprintf(L"DgnElementSchema not found \n");
    return;
    }

ECClassP lineElementClassP = elementSchemaPtr.get()->GetClassP(L"LineElement");
if (NULL == lineElementClassP)
    {
    wprintf(L"LineElement Class pointer not found \n");
    return;
    }

WString steTotallength = L"TotalLength";
WString strColor = L"Color";
WString strLevel = L"Level";

ECValue totalLengthWithoutTypeAdapter;
ECValue colorWithoutTypeAdapter;
ECValue levelWithoutTypeAdapter;

WString totalLengthWithTypeAdapter(L"");
WString colorWithTypeAdapter(L"");
WString levelWithTypeAdapter(L"");

ECQueryPtr query = ECQuery::CreateQuery(*lineElementClassP, true);
for (DgnECInstancePtr dgnECInstance : DgnECManager::GetManager().FindInstances(*instanceScopePtr, *query))
    {
    dgnECInstance->GetValue(totalLengthWithoutTypeAdapter, steTotallength.c_str());
    dgnECInstance->GetValue(colorWithoutTypeAdapter, strColor.c_str());
    dgnECInstance->GetValue(levelWithoutTypeAdapter, strLevel.c_str());

    //GetValueAsString() supports array , 3rd argument is for array and 4th arguments is for array index 
    dgnECInstance->GetValueAsString(totalLengthWithTypeAdapter, steTotallength.c_str(), false, 0);
    dgnECInstance->GetValueAsString(colorWithTypeAdapter, strColor.c_str(), false, 0);
    dgnECInstance->GetValueAsString(levelWithTypeAdapter, strLevel.c_str(), false, 0);
    }

WString strResultWithoutTypeAdapter;
strResultWithoutTypeAdapter.Sprintf(L"%ls -> %ls : %f, %ls : %d, %ls : %d \n", L"ECValue Without TypeAdapter", steTotallength.c_str(),
                                    totalLengthWithoutTypeAdapter.GetDouble(), strColor.c_str(), colorWithoutTypeAdapter.GetInteger(), strLevel.c_str(), levelWithoutTypeAdapter.GetInteger());
mdlOutput_messageCenter(OutputMessagePriority::Info, (strResultWithoutTypeAdapter.c_str()), NULL, OutputMessageAlert::None);

WString strResultWithTypeAdapter;
strResultWithTypeAdapter.Sprintf(L"%ls -> %ls : %ls, %ls : %ls, %ls : %ls \n", L"ECValue with TypeAdapter", steTotallength.c_str(),
             totalLengthWithTypeAdapter.c_str(), strColor.c_str(), colorWithTypeAdapter.c_str(), strLevel.c_str(), levelWithTypeAdapter.c_str());
    mdlOutput_messageCenter(OutputMessagePriority::Info, (strResultWithTypeAdapter.c_str()), NULL, OutputMessageAlert::None);

Execute Key-in :
Managed :
ECValueWithTypeAdapterManaged CreateElementAndGetECValue
Native
ECValueWithTypeAdapter NativeExample CreateElementAndGetECValue

After executing the key-in, line element will be created and the message center will display property values for TotalLength, Color, and Level property.
Results will be show as below :