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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:21:01+00:00 2026-05-13T20:21:01+00:00

Here is my situation. I am working with WMI in C#. Thankfully, I found

  • 0

Here is my situation. I am working with WMI in C#. Thankfully, I found MgmtClassGen.exe which generated all of the classes I need for now.

However, I have been going through the auto generated classes, and ripping out the common code to either a Utility class, or a base class. So far so good, got a lot of the code cleaned up. But I’ve hit a snag. Each class has several (about 8) static functions called GetInstances. Basically there are 2 overloads, and the other function are just providing default parameters.

I would like to put these function in the base class because they are identical across all the classes, except for 3 variables. Namely, the ClassName (like “MicrosoftDNS_Zone“) of the object, the Namespace (like “root\microsoftdns“) of the Object, and a ManagementScope object.

What I have done currently is this; Moved the 2 functions with code in them to the base class and added 3 parameters, for the 3 differences listed above. But this still requires 8 functions in each class that just call the base class with the ClassName, Namespace, and Scope parameters filled in.

  1. Is there any way to refactor this
    code so that I don’t need the method
    “wrappers” in each derived class?
  2. Can I just declare the static
    methods in the base class and
    somehow get the ClassName, etc. from
    the derived class?
  3. Would reflection even work here?

Base Class:

namespace WMI
{
    public abstract class Object : System.ComponentModel.Component
    {
        // ...
        protected static WMI.ManagementTypeCollection GetInstances( string className, string namespaceName, ManagementScope statMgmtScope, ManagementScope mgmtScope, EnumerationOptions enumOptions, Func<ManagementObject,WMI.Object> del )
        {
            if( (mgmtScope == null) )
            {
                if( (statMgmtScope == null) )
                {
                    mgmtScope = new System.Management.ManagementScope();
                    mgmtScope.Path.NamespacePath = namespaceName;
                }
                else
                {
                    mgmtScope = statMgmtScope;
                }
            }
            System.Management.ManagementPath pathObj = new System.Management.ManagementPath();
            pathObj.ClassName = className;
            pathObj.NamespacePath = namespaceName;
            System.Management.ManagementClass clsObject = new System.Management.ManagementClass( mgmtScope, pathObj, null );
            if( (enumOptions == null) )
            {
                enumOptions = new System.Management.EnumerationOptions();
                enumOptions.EnsureLocatable = true;
            }
            return new WMI.ManagementTypeCollection( clsObject.GetInstances( enumOptions ), del );
        }

        protected static WMI.ManagementTypeCollection GetInstances( string className, string namespaceName, ManagementScope statMgmtScope, ManagementScope mgmtScope, string condition, String[] selectedProperties, Func<ManagementObject, WMI.Object> del )
        {
            if( (mgmtScope == null) )
            {
                if( (statMgmtScope == null) )
                {
                    mgmtScope = new System.Management.ManagementScope();
                    mgmtScope.Path.NamespacePath = namespaceName;
                }
                else
                {
                    mgmtScope = statMgmtScope;
                }
            }
            System.Management.ManagementObjectSearcher ObjectSearcher = new System.Management.ManagementObjectSearcher( mgmtScope, new SelectQuery( className, condition, selectedProperties ) );
            System.Management.EnumerationOptions enumOptions = new System.Management.EnumerationOptions();
            enumOptions.EnsureLocatable = true;
            ObjectSearcher.Options = enumOptions;
            return new WMI.ManagementTypeCollection( ObjectSearcher.Get(), del );
        }
    }
}

Derived Class:

namespace WMI.MicrosoftDNS
{
    public class AAAAType : WMI.Object
    {
        private static string CreatedWmiNamespace = "root\\microsoftdns";
        private static string CreatedClassName = "MicrosoftDNS_AAAAType";
        private static System.Management.ManagementScope statMgmtScope = null;

        // ...

        public static WMI.ManagementTypeCollection GetInstances()
        {
            return GetInstances( null, null, null );
        }

        public static WMI.ManagementTypeCollection GetInstances( string condition )
        {
            return GetInstances( null, condition, null );
        }

        public static WMI.ManagementTypeCollection GetInstances( System.String[] selectedProperties )
        {
            return GetInstances( null, null, selectedProperties );
        }

        public static WMI.ManagementTypeCollection GetInstances( string condition, System.String[] selectedProperties )
        {
            return GetInstances( null, condition, selectedProperties );
        }

        public static WMI.ManagementTypeCollection GetInstances( ManagementScope mgmtScope, EnumerationOptions enumOptions )
        {
            return WMI.Object.GetInstances( CreatedClassName, CreatedWmiNamespace, statMgmtScope, mgmtScope, enumOptions, mo => new AAAAType( mo ) );
        }

        public static WMI.ManagementTypeCollection GetInstances( ManagementScope mgmtScope, string condition )
        {
            return GetInstances( mgmtScope, condition, null );
        }

        public static WMI.ManagementTypeCollection GetInstances( ManagementScope mgmtScope, System.String[] selectedProperties )
        {
            return GetInstances( mgmtScope, null, selectedProperties );
        }

        public static WMI.ManagementTypeCollection GetInstances( ManagementScope mgmtScope, string condition, System.String[] selectedProperties )
        {
            return WMI.Object.GetInstances( CreatedClassName, CreatedWmiNamespace, statMgmtScope, mgmtScope, condition, selectedProperties, mo => new AAAAType( mo ) );
        }
    }
}
  • 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-13T20:21:02+00:00Added an answer on May 13, 2026 at 8:21 pm

    Why not move these eight common functions into a separate class as instance methods, and initialize the instance with the three parameters that vary by class. Then you could aggregate and expose the instance of that helper class on each of the derived types. Since the static methods can’t use any instance data, there’s no reason to duplicate them.

    Here’s an example of what I mean:

    public class WMIInstance
    {
        private readonly string CreatedWmiNamespace = "root\\microsoftdns";
        private readonly string CreatedClassName = "MicrosoftDNS_AAAAType";
        private readonly System.Management.ManagementScope statMgmtScope = null;
    
        public WMIInstance( string namespace, string className, ManagementScope scope )
        { /*... initialize private members here... */
    
        public WMI.ManagementTypeCollection GetInstances( System.String[] selectedProperties )
        { return WMI.Object.GetInstances( ... ); }
    
        /* other overloads ... */
    }
    
    public class AAAAType : WMI.Object
    {
        private static string CreatedWmiNamespace = "root\\microsoftdns";
        private static string CreatedClassName = "MicrosoftDNS_AAAAType";
        private static System.Management.ManagementScope statMgmtScope = null;
    
        private static readonly WMIInstances _instances = new WMIInstance( CreatedWmiNamespace, CreatedClassName, statMgmgtScope );
    
        public static WMIInstances Getter { get { return _instances; } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation: the company that I'm working in right now gave me
Here's the situation that I'm working with: Build tests in Selenium Get all the
Here's the situation: I have RESTEasy and Jackson working together beautifully My RESTful classes
Here's the situation: I'm working on an application which allows automated management of network
Here is my situation. I need to create a report that shows each open
Ok, so here's the situation. I'm working on a site that allows people to
Here's the situation: I'll be coding away in VS 2010, when all of a
Here's my situation: I'm working on a PHP project that uses a few functions
Here's the situation. I'm working on the master branch. I create file1 and commit.
Here's my situation: I have been working on an ASP.NET MVC 3 application for

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.