Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 541769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:22:37+00:00 2026-05-13T10:22:37+00:00

I must honestly say that I do not really understand much about casting, but

  • 0

I must honestly say that I do not really understand much about casting, but I thought this would work.

I have a Generic Class Test:

public class Test<T,U>
{
     T variable1;
     U variable2;

     //etc.
}

I need to use this class in a WPF view, and since you can’t create generic views in WPF (how lovely) I thought: lets just use a Test<object, object> for the view, since I am only concerned about working with the string representations of the variables in the view.

So I try:

  Test<Foo, Bar> test = new Test<Foo, Bar>();
  return test as Test<object, object>;

But that gives me:

  Error 1   Cannot convert type 'DomainModel.Tests.Test<T,U>' 
  to 'DomainModel.Tests.Test<object,object>' via a reference conversion, boxing
  conversion, unboxing conversion, wrapping conversion, or null type conversion

I would think that every object must be castable to object?

Anyhow, I am quite stuck now in how to ever use generic classes in WPF…

Any pointer in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:22:38+00:00Added an answer on May 13, 2026 at 10:22 am

    This is a generic variance issue. C# 3 does not allow variance in generic parameter types: thus, IEnumerable<string> is not compatible with IEnumerable<object>. This restriction will be relaxed for safe scenarios in C# 4. But this won’t help for your case because C# 4 only supports variance on interfaces or delegates. And even if a future version of C# allows generic variance on classes, it still may not help you because it’s not clear that Test<object, object> is a safe cast: suppose variable1 has a setter:

    Test<string, string> t = new Test<string, string>();
    Test<object, object> bad_t = t as Test<object, object>;
    bad_t.variable1 = new Llama();  // but variable1 should contain only strings!
    

    You can however “cast” a Test<string, string> to object:

    object good_t = t;  // don't even need a cast
    

    WPF bindings will still then be able to access its properties via reflection.

    Alternatively, define a non-generic interface that provides the functionality you need (in your case “the string representations of the variables”):

    public interface IPresentV1AndV2  // stupid name of course
    {
      public string Variable1AsString { get; }
      public string Variable2AsString { get; }
    }
    

    and implement this on your generic class. You can then access the stringised values via the interface members. (Obviously this particular interface example is stupidly bound to the implementation and in reality you would give it a name and members that reflected the business meaning of the data it exposes.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There must be an easy way to do this, but somehow I can wrap
This is not a real situation; please ignore legal issues that you might think
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I must admit that I never understood what are the streams are all about-
I must be getting daft, but I can't seem to find how to read
This must be a classic .NET question for anyone migrating from Java. .NET does
So I haven't been doing this for long but I'm completely stuck on this.
This must be simple. I am trying to pass sub-element into a JSF component.
There must be a simpler, more pythonic way of doing this. Given this list
It must be Monday, the heat or me being stupid (prob the latter), but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.