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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:17:35+00:00 2026-05-14T02:17:35+00:00

First of all, sorry if this has been asked before. I’ve done a pretty

  • 0

First of all, sorry if this has been asked before. I’ve done a pretty comprehensive search and found nothing quite like it, but I may have missed something.

And now to the question: I’m trying to invoke a constructor through reflection, with no luck. Basically, I have an object that I want to clone, so I look up the copy constructor for its type and then want to invoke it. Here’s what I have:

public Object clone(Object toClone) {
     MethodBase copyConstructor = type.GetConstructor(
         new Type[] { toClone.GetType() });
     return method.Invoke(toClone, new object[] { toClone }); //<-- doesn't work
}

I call the above method like so:

List<int> list = new List<int>(new int[] { 0, 1, 2 });
List<int> clone = (List<int>) clone(list);

Now, notice the invoke method I’m using is MethodBase‘s invoke. ConstructorInfo provides an invoke method that does work if invoked like this:

return ((ConstructorInfo) method).Invoke(new object[] { toClone });

However, I want to use MethodBase‘s method, because in reality instead of looking up the copy constructor every time I will store it in a dictionary, and the dictionary contains both methods and constructors, so it’s a Dictionary<MethodBase>, not Dictionary<ConstructorInfo>.
I could of course cast to ConstructorInfo as I do above, but I’d rather avoid the casting and use the MethodBase method directly. I just can’t figure out the right parameters.

Any help? Thanks so much.


EDIT

Benjamin,
Thanks so much for your suggestions. I was actually doing exactly what you suggest in your second edit, except (and that’s a big “except”) my dictionary was where

class ClonerMethod {

    public MethodBase method;
    public bool isConstructor;

    ...

    public Object invoke(Object toClone) {
        return isConstructor ?
            ((ConstructorInfo) method).Invoke(new object[] { toClone }) : //<-- I wanted to avoid this cast
            method.Invoke(toClone, null);
    }

}

And then I called ClonerMethod‘s invoke on what I found in the dictionary. I didn’t add the code the deals with all that because the answer I was looking for was just how to call Invoke on a ConstructorInfo using MethodBase‘s Invoke method, so I didn’t want to add unnecessary info and too much code for you guys to read through. However, I like your use of Func<,> much MUCH better, so I’m switching to that. Also making the Clone method generic is a nice addition, but in my case the caller doesn’t know the type of the object, so I’ll keep it non-generic instead.

I didn’t know about Func<,>, and if I knew about the lambda operator I had forgotten (I hadn’t really needed something like this before), so I’ve actually learnt a lot from your answer. I always love to learn new things, and this will come in very handy in the future, so thanks a lot! 🙂

  • 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-14T02:17:36+00:00Added an answer on May 14, 2026 at 2:17 am

    If you know that the object is having a constructor like that, did you think about using this overload of Activator.CreateInstance instead?


    Update: So you have a cascading search for MethodInfo/MethodBase already and store them -> You don’t want/cannot use Activator.

    In that case I don’t see a way to do what you want without a cast. But – maybe you could change the architecture to store a Dictionary<Type, Func<object, object>> and add those Func<> instances instead. Makes the calling code nicer (I assume) and would allow you to do this cast once:

    // Constructor
    dictionary.Add(type,
      source => ((ConstructorInfo) method).Invoke(new object[] {source})
    );
    
    // Clone
    dictionary.Add(type,
      source => method.Invoke(source, new object[]{})
    );
    

    In fact, since you only care about the difference between constructor and normal method at the very site where you grab them, you wouldn’t need a cast at all, would you?

    // Constructor 2
    dictionary.Add(type,
      source => yourConstructorInfo.Invoke(new object[] {source})
    );
    

    Unless I’m missing something (quite possible, of course) this could resolve the problem by doing this once on the defining side of the fence and the caller wouldn’t need to mind if this is constructor or not?


    One last time, then I’m going to stop the edit spam. I was bored and came up with the following code. Is that what you are trying to accomplish?

    public class Cloner {
        private readonly IDictionary<Type, Func<object, object>> _cloneMap =
                new Dictionary<Type, Func<object, object>>();
    
        public T Clone<T>(T source) {
            Type sourceType = source.GetType();
            Func<object, object> cloneFunc;
    
            if (_cloneMap.TryGetValue(sourceType, out cloneFunc)) {
                return (T)cloneFunc(source);
            }
    
            if (TryGetCopyConstructorCloneFunc(sourceType, out cloneFunc)) {
                _cloneMap.Add(sourceType, cloneFunc);
                return (T)cloneFunc(source);
            }
    
            if (TryGetICloneableCloneFunc(sourceType, out cloneFunc)) {
                _cloneMap.Add(sourceType, cloneFunc);
                return (T)cloneFunc(source);
            }
    
            return default(T);
        }
    
        private bool TryGetCopyConstructorCloneFunc(Type type, 
                        out Func<object, object> cloneFunc) {
            var constructor = type.GetConstructor(new[] { type });
            if (constructor == null) {
                cloneFunc = source => null;
                return false;
            }
            cloneFunc = source => constructor.Invoke(new[] { source });
            return true;
        }
    
        private bool TryGetICloneableCloneFunc(Type type,
                        out Func<object, object> cloneFunc) {
            bool isICloneable = typeof(ICloneable).IsAssignableFrom(type);
            var cloneMethod = type.GetMethod("Clone", new Type[] { });
            if (!isICloneable || (cloneMethod == null)) {
                cloneFunc = source => null;
                return false;
            }
            cloneFunc = source => cloneMethod.Invoke(source, new object[] {});
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all; sorry if this has been asnwered before, I have searched for
First of all I'm sorry if you feel this question has been raised before,
I'm sorry if this questions has been asked before, but I couldn't find an
Once again, sorry if this has been asked before and if its too specific
First of all I am sorry if such problem has been asked because I
Well, first of all sorry about this question it must be pretty straight forward
First of all sorry if the question has been already posted, but I didn't
Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
First of all, I'm fairly new to Java, so sorry if this question is

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.