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 I have three classes. It is valid to instantiate A, but there are
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a directory /dir inside which there are 3 symlinks to other
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a SELECT statement that returns some set of results. Is there
There's no strongly typed View() method to return an ActionResult. So, suppose I have
I have a simple question. Is there a way ( using reflections I suppose
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table

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.