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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:32:50+00:00 2026-05-16T12:32:50+00:00

I am trying to write a generic extension to turn a ManagementObjectCollection into a

  • 0

I am trying to write a generic extension to turn a ManagementObjectCollection into a DataTable. This is just to make things easier for a startup script/program I am writing. I have ran into a problem with CimType. I have included the code I have written so far below.

    public static DataTable GetData(this ManagementObjectCollection objectCollection)
    {
        DataTable table = new DataTable();

        foreach (ManagementObject obj in objectCollection)
        {
            if (table.Columns.Count == 0)
            {
                foreach (PropertyData property in obj.Properties)
                {
                    table.Columns.Add(property.Name, property.Type);
                }
            }

            DataRow row = table.NewRow();

            foreach (PropertyData property in obj.Properties)
            {
                row[property.Name] = property.Value;
            }

            table.Rows.Add(row);
        }

        return table;
    }
}

I have found the a method which I think will work at http://www.devcow.com/blogs/adnrg/archive/2005/09/23/108.aspx. However it seems to me like there may be a better way, or even a .net function I am overlooking.

I guess I didn’t make it clear. The problem I am having is that I need to convert from System.Management.CimType to System.Type. I almost thought this would be a common problem, but I suppose I’m trying to solve it in a general way.

  • 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-16T12:32:50+00:00Added an answer on May 16, 2026 at 12:32 pm

    Hi You can also try the following code:

    public static class CimConvert
    {
    
    private readonly static IDictionary<CimType, Type> Cim2TypeTable =
        new Dictionary<CimType, Type>
            {
                {CimType.Boolean, typeof (bool)},
                {CimType.Char16, typeof (string)},
                {CimType.DateTime, typeof (DateTime)},
                {CimType.Object, typeof (object)},
                {CimType.Real32, typeof (decimal)},
                {CimType.Real64, typeof (decimal)},
                {CimType.Reference, typeof (object)},
                {CimType.SInt16, typeof (short)},
                {CimType.SInt32, typeof (int)},
                {CimType.SInt8, typeof (sbyte)},
                {CimType.String, typeof (string)},
                {CimType.UInt8, typeof (byte)},
                {CimType.UInt16, typeof (ushort)},
                {CimType.UInt32, typeof (uint)},
                {CimType.UInt64, typeof (ulong)}
            };
    
    public static Type Cim2SystemType(this PropertyData data)
    {
        Type type = Cim2TypeTable[data.Type];
        if (data.IsArray)
            type = type.MakeArrayType();
        return type;
    }
    
    public static object Cim2SystemValue(this PropertyData data)
    {
        Type type = Cim2SystemType(data);
        if (data.Type == CimType.DateTime)
            return DateTime.ParseExact(data.Value.ToString(), "yyyyMMddHHmmss.ffffff-000", CultureInfo.InvariantCulture);
        return Convert.ChangeType(data.Value, type);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a generic extension method that let's me do this: this.startDate
I am trying to write a generic method that will convert a DataTable to
I am trying to write a generic library in pure c , just some
I am trying to write a generic method for deserializing json into my model.
I'm trying to write a generic function to start and end events into my
I'm trying to write a generic metaclass to track subclasses Since I want this
I am trying to write a generic extension method for adding a fixed matrix
I am trying to write a generic method that would bind any asp.net UI
I'm trying to write a generic function that works with JSON arrays containing arbitrary
I'm trying to write a generic class which contains a generic TObjectList< T >

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.