locked
check if a Platform::Object is an instance of certain ref class?

    Question

  • Foo A;

    auto B = safe_cast<Platform::Object^>(A);

    Is there a way to check B is an instance of Foo?

    Thanks,

    Helin

    Saturday, January 5, 2013 11:55 PM

Answers

  • We can use Type::FullName property to get the instance, like

    	auto B=safe_cast<Platform::Object^>(a);
    	String^ name=B->GetType()->FullName;

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Thursday, January 10, 2013 3:10 AM
    • Marked as answer by Jesse Jiang Friday, January 11, 2013 3:28 AM
    Monday, January 7, 2013 8:23 AM
  • Platform::Object^ a = ref new Foo();
    if (dynamic_cast<Foo^>(a) != nullptr)
    {
        // *a is a Foo object
    }
    
    dynamic_cast yields nullptr if the pointed-to object is not of the target type.  Its behavior is otherwise similar to safe_cast.
    • Proposed as answer by Jesse Jiang Thursday, January 10, 2013 3:10 AM
    • Marked as answer by Jesse Jiang Friday, January 11, 2013 3:28 AM
    Monday, January 7, 2013 8:20 PM

All replies

  • We can use Type::FullName property to get the instance, like

    	auto B=safe_cast<Platform::Object^>(a);
    	String^ name=B->GetType()->FullName;

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Thursday, January 10, 2013 3:10 AM
    • Marked as answer by Jesse Jiang Friday, January 11, 2013 3:28 AM
    Monday, January 7, 2013 8:23 AM
  • thanks Jesse! For bringing me a working way.

    Is there a way does not involve String comparison?

    Like:

    Foo A

    auto B=safe_cast<Platform::Object^>(A);

    bar(B) == baz(Foo) ?

    Monday, January 7, 2013 7:40 PM
  • Platform::Object^ a = ref new Foo();
    if (dynamic_cast<Foo^>(a) != nullptr)
    {
        // *a is a Foo object
    }
    
    dynamic_cast yields nullptr if the pointed-to object is not of the target type.  Its behavior is otherwise similar to safe_cast.
    • Proposed as answer by Jesse Jiang Thursday, January 10, 2013 3:10 AM
    • Marked as answer by Jesse Jiang Friday, January 11, 2013 3:28 AM
    Monday, January 7, 2013 8:20 PM
  • Thanks! That's better performance wise.

    Is there a language wise reason that I can't do it like C# "if A is Foo"?

    Wednesday, January 9, 2013 9:11 PM
  • "is" is not the keywords in C++ or C++/CX, so that we can not do it as C#.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 3:10 AM