none
Converting System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily RRS feed

  • Question

  • Hi Guys,

    Can I cast System.Drawing.FontFamily to System.Windows.Media.FontFamily and vice versa

    e.g.

    System.Windows.Media.FontFamily ff = new System.Drawing.FontFamily("Arial") as System.Windows.Media.FontFamily;

    or

    System.Windows.Media.FontFamily ff = ( System.Windows.Media.FontFamily) new System.Drawing.FontFamily("Arial");

    Thanks & Regards,

    Thursday, April 2, 2015 8:36 PM

Answers

  • Hello pkjse,

    >>Can I cast System.Drawing.FontFamily to System.Windows.Media.FontFamily and vice versa

    No, we cannot do it and I think we would encounter an exception as:

    Cannot convert type 'System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

    Or

    Cannot convert type 'System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily

    In .NET reference types can be casted to any one of its direct or indirect base classes or interfaces, and we can see that these two class are under different namespaces and does not have any inheritance relationship. For details, you could refer to this link:

    Casting and Type Conversions (C# Programming Guide)

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 3, 2015 8:01 AM
    Moderator

All replies

  • Hello pkjse,

    >>Can I cast System.Drawing.FontFamily to System.Windows.Media.FontFamily and vice versa

    No, we cannot do it and I think we would encounter an exception as:

    Cannot convert type 'System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

    Or

    Cannot convert type 'System.Drawing.FontFamily' to 'System.Windows.Media.FontFamily

    In .NET reference types can be casted to any one of its direct or indirect base classes or interfaces, and we can see that these two class are under different namespaces and does not have any inheritance relationship. For details, you could refer to this link:

    Casting and Type Conversions (C# Programming Guide)

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 3, 2015 8:01 AM
    Moderator
  • Hi Guys,

    I think we should get the Type of System.Drawing.FontFamily or System.Windows.Media.FontFamily firstly, and then create instance of System.Windows.Media.FontFamily OR System.Drawing.FontFamily.

    You can write a extension method to achieve this goal.

    Thanks


    The future belongs to those who believe in the beauty of their dreams.

    Friday, April 3, 2015 8:58 AM