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

  • Home
  • SEARCH
  • 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 6530799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:58+00:00 2026-05-25T09:47:58+00:00

I have function as below . public static object getClassInstance(string key, object constructorParameter) {

  • 0

I have function as below .

public static object getClassInstance(string key, object constructorParameter)
{
         // body here
}

Key variable will have my class name . I need to return the new instance of the class . If the constructorParm is null then i need to load the class with default constructor else with the constructor parameter passed. How do i do this ?

ADD :

I wrote the code like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Catalyst.BO.StudentProfileBO;
using Catalyst.BO.ReportBO;
using Catalyst.DAL.ReportDAO;
using System.Collections;
using System.Data;

namespace Catalyst.BO.Factory
{
    public class CFactory
    {
        public static object getClassInstance(string key, params  object[] constructorArgs)
        {
            string assemblyPath = null;
            string customClassName = key.Substring(0, 1) + "Custom" + key.Substring(1);

            DataSet objDataset = getAssemblyInfo(key);
            if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0)
            {
                assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString();
            }

            Assembly assembly;
            Type type;

            if (assemblyPath != null && assemblyPath != string.Empty)
            {
                assembly = Assembly.LoadFile(assemblyPath);
                type = assembly.GetType(customClassName);
            }
            else // if no customisation
            {
                type = Type.GetType(key);
            }

            object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs);
            if (classInstance == null) throw new Exception("broke");
            return classInstance;
        }
    }
}

key passed to the function is “CReportBO”. CReportBO is accessible in the function’s scope . but in //if no customization section (i.e type = Type.GetType(key) ) , type returns me null . whats wrong ?

  • 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-25T09:47:58+00:00Added an answer on May 25, 2026 at 9:47 am

    If key is either assembly-qualified, or namespace-qualified within either the core assemblies or the calling assembly, then:

    Type type = Type.GetType(key);
    return constructorParameter == null ? Activator.CreateInstance(type)
              : Activator.CreateInstance(type, constructorParameter);
    

    I wonder, though, if:

    public static object getClassInstance(string key, params object[] args)
    

    is more flexible, allowing:

    Type type = Type.GetType(key);
    return Activator.CreateInstance(type, args);
    

    with usage such as:

    object o = getClassInstance(key); // uses default constructor
    object o = getClassInstance(key, null); // passes null to single parameter
    object o = getClassInstance(key, 123, "abc"); // etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
I have the below function public static List<DateTime> GetOnlyFridays(DateTime endDate, int weeks, bool isIncludeBaseDate)
I have the class. Class User { private $_name; private $_email; public static function
I have the following function below: public function setupHead($title){ $displayHead .='<!DOCTYPE html PUBLIC -//W3C//DTD
I have an example function below that reads in a date as a string
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
I have defined two classes below: public class junk { private BigInteger a =
i have the code as below : class Singleton { private: int i; static
I have a static function in a class with two overloads. Both the overloads
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment

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.