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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:52:46+00:00 2026-05-12T11:52:46+00:00

Suppose I have three projects in my sln. (1) xyz.a{Class Lib}{no reference added} (2)

  • 0

Suppose I have three projects in my sln.

(1) xyz.a{Class Lib}{no reference added}
(2) yzx.b{Class Lib}{added the reference of xyz.a}
(3) zxy.c{Console App}{added the reference of xyz.a}

Now, I need to create the instance of a class residing in yzx.b from within xyz.a using reflection.

And also this should be independent of the folder/directory-names.

I.e. even If I change the name of the directory of yzx.b, it should work.

Does anyone have any idea?

  • 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-12T11:52:46+00:00Added an answer on May 12, 2026 at 11:52 am

    First of all, Activator.CreateInstance() is a right way.

    But, there is a more interesting way that is:

    • 10 times faster
    • Don’t wrap exceptions in
      TargetInvocationException

    Just create expression that calls constructor:

    public static Func<object[], object> CreateConstructorDelegate(ConstructorInfo method)
    {
            var args = Expression.Parameter(typeof(object[]), "args");
    
            var parameters = new List<Expression>();
    
            var methodParameters = method.GetParameters().ToList();
            for (var i = 0; i < methodParameters.Count; i++)
            {
                parameters.Add(Expression.Convert(
                                   Expression.ArrayIndex(args, Expression.Constant(i)),
                                   methodParameters[i].ParameterType));
            }
    
            var call = Expression.Convert(Expression.New(method, parameters), typeof(object));
    
            Expression body = call;
    
            var callExpression = Expression.Lambda<Func<object[], object>>(body, args);
            var result = callExpression.Compile();
    
            return result;
    }
    

    Performance test:

        public void activator()
        {
            var stopwatch = new Stopwatch();
            const int times = 10000000;
    
            stopwatch.Start();
            for (int i = 0; i < times; i++)
            {
                var v = Activator.CreateInstance(typeof (C));
            }
            stopwatch.Stop();
    
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms with activator");
    
            var del = CreateConstructorDelegate(typeof(C).GetConstructor(new Type[0]));
    
            stopwatch = new Stopwatch();
            stopwatch.Start();
    
            var args = new object[0];
    
            for (int i = 0; i < times; i++)
            {
                var v = del(args);
            }
    
            stopwatch.Stop();
    
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms with expression");
        }
    

    Output:

    1569ms with activator
    134ms with expression
    

    But:

    • C# 3.0 only
    • Complile() is long running operation

    Just for curious.

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

Sidebar

Related Questions

Suppose that we have previously instantiated three objects A, B, C from class D
Suppose we have the following (Visual Studio) projects (simplified): base-lib-1 base-lib-2 product-1 : depending
Suppose I have this simple app working : # model class Project has_many :numbers
Suppose I have three lists: list1 = a, c, d, r, t list2 =
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
In the /data directory of solr, suppose, I have three folders named as index,
Suppose I have a string like this: one two three four five six seven
Suppose I have an HTML page with three blocks of fixed width (their height

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.