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 8786593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:40:31+00:00 2026-06-13T21:40:31+00:00

Not much to say, This is the issue: public static T WriteIfNotNull<T>(ManagementObject retObject, string

  • 0

Not much to say,
This is the issue:

    public static T WriteIfNotNull<T>(ManagementObject retObject, string parameter)
    {
        return retObject[parameter] != null ? (T)retObject[parameter] : default(T);
    }

It breaks when T is an int and retObject[parameter] is an object. The odd part (for me at least) is that if T is not T and is actually a specified int it does work.

It says it can’t be casted to T (even that in this case T is int).

@Edit: @dtb here it goes:

Specified cast is not valid. 
(T)(object)retObject[parameter] Cannot unbox 'retObject[parameter]' as a 'T'    int         

And the values:

parameter: "ProcessId"

retObject[parameter]: 4

default(T): 0

@@Edit: Here’s the call too

pr.ProcessId = Util.WriteIfNotNull<int>(retObject, "ProcessId");
  • 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-06-13T21:40:33+00:00Added an answer on June 13, 2026 at 9:40 pm

    It seems to me that what’s happening is that retObject[parameter] isn’t actually an int but is another numeric type (uint for process IDs typically), in which case you’d want to use Convert to actually change the value:

    return retObject[parameter] != null 
        ? (T)Convert.ChangeType(retObject[parameter], typeof(T)) 
        : default(T);
    

    The reason a simple cast doesn’t work here is because casting from object to int only works if it’s actually a boxed int. You can’t do a downcast and conversion cast in one operation because there is not a conversion cast from object to int. Thus casting away from object requires you to cast to the exact type or a valid super-type, interface, etc of the exact type.

    So, to convert a boxed uint to an int we’d have to either unbox as an int first (problematic) or use one of the Convert methods to change the type (as shown above).

    Convert.ChangeType() works for converting between compatible types even if they are boxed. For example, if retObject[parameter] returns a long then this will allow it to be converted to a T of int.

    I have a blog post on this here with more detail, hopefully this makes sense. The gist, again, is that casting from object is a downcast (no conversion casts are defined for object) thus you must cast it to it’s precise type (or a valid sub-type).

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

Sidebar

Related Questions

Not much to say about this question...
The documentation does not say much besides: Private: Changes made to the resulting buffer
I'm not much into COM interfaces, so i have a small question, say I
This is driving me crazy and has resulted in lost work (not much, at
I'm not sure exactly how much this falls under 'programming' opposed to 'program language
I ran into this issue quite a few times. Let's say I have a
not much of a programming question, but development related still, I'm starting Android development
Not much code since I'm a bit at a loss on how to start.
I'm not much of a programmer but I want to integrate http://storify.com/ into my
I am not much experienced in boost::asio . I've some pretty basic questions. Do

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.