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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:15:19+00:00 2026-05-12T09:15:19+00:00

Forgive me if this question has already been asked and answered. Given a class

  • 0

Forgive me if this question has already been asked and answered.

Given a class of type T, what is the difference between the following?

T myObj = Activator.CreateInstance<T>();

T myObj = typeof(T).InvokeMember(null, BindingFlags.CreateInstance, null, null, null);

Is one solution preferred over the other?

  • 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-12T09:15:19+00:00Added an answer on May 12, 2026 at 9:15 am

    Decompiling RuntimeType.InvokeMember yields this fragment:

    if ((bindingFlags & BindingFlags.CreateInstance) != BindingFlags.Default)
    {
        if (((bindingFlags & BindingFlags.CreateInstance) != BindingFlags.Default) && ((bindingFlags & (BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.SetField | BindingFlags.GetField | BindingFlags.InvokeMethod)) != BindingFlags.Default))
        {
            throw new ArgumentException(Environment.GetResourceString("Arg_CreatInstAccess"), "bindingFlags");
        }
        return Activator.CreateInstance(this, bindingFlags, binder, providedArgs, culture);
    }
    

    In other words, InvokeMember with those BindingFlags calls Activator.CreateInstance. It goes through several more call layers (checking bindings, verifying arguments) before getting down to business. Activator.CreateInstance<T> is much more succinct:

    public static T CreateInstance<T>()
    {
        bool bNeedSecurityCheck = true;
        bool canBeCached = false;
        RuntimeMethodHandle emptyHandle = RuntimeMethodHandle.EmptyHandle;
        return (T) RuntimeTypeHandle.CreateInstance(typeof(T) as RuntimeType, true, true, ref canBeCached, ref emptyHandle, ref bNeedSecurityCheck);
    }
    

    EDITED You might expect the latter to be faster, but a method called RuntimeType.CreateInstanceSlow also calls RuntimeTypeHandle.CreateInstance to do the work; it’s used as a fallback if an Activator cache entry for the constructor can’t be found. I’d do some performance testing if you’re looking for the fastest solution of the two.

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

Sidebar

Related Questions

Please forgive me if this question has been asked and answered already. If so,
Forgive me if this question has been asked before but I am new to
Please forgive if this question has been asked numerous times. I recently installed Eclipse
Please forgive me if this question has been answered - I searched and couldn't
Forgive me if this has already been asked elsewhere. I have a Scala syntax
Forgive me if this has already been asked, but I'm new to C# and
First, forgive me if this has been answered already. I spent a few hours
This is my first post, so please forgive me if this question has been
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
Forgive me if this has been asked before, but I was unable to find

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.