2

I know that FormatterServices.GetUninitializedObject can do that, but the core library for xbox does not contain the class.

I'm working on a XNA game and I'm trying to make this work there. Activator.CreateInstance is available but than I need a default constructor, what's ugly.

I thought of if there is no default constructor, I take the shortest and pass in any values (for example when it requires an int and an object, I pass in 0 and null). But the problem on that is that the constructor could throw an exception if it works with the objects passed in. Well I still could write it in a try catch block and maybe it works that way. But I really unlikely would do that. Is there any other solution?

Thanks for any help :)

2
  • Why do you want to do that? Commented Mar 23, 2013 at 16:31
  • What would you do with an object that isn't initialised properly? Commented Mar 23, 2013 at 16:34

3 Answers 3

4

You don't need the default constructor, I think there's an overload where you can pass parameters in

Activator.CreateInstance (typeof (Foo), new object []{  args});

That said, you can also use reflection to get the constructor and just call that.

Edit: wait - rereading the question, are you saying you don't know what to pass in? Then why are you creating it?

Sign up to request clarification or add additional context in comments.

1 Comment

That worked. Thanks man. -- Dunno what is going on with Visual Studio lately, seems like it should have showed me that overload -- still having trouble finding it in the intelli-sense, btw -- but it works, so that's awesome. :-)
4

No, this is completely the wrong solution.

You definitely need a default constructor, otherwise you have no business constructing the objects this way.

Note that you can use a "non-default" (I'm taking "default" here as meaning "parameterless") constructor, but you should still pass in the required parameters, not just by type, but by meaning as well.

Since you're resorting to Activator.CreateInstance, I'm assuming you don't know a lot about the type in question, except its Type object. In this (general) case, you should not just pass in some odd values to the constructor in the hopes that it will make sense to the type in question.

If, on the other hand, you have some sort of contract going, where you can say "I know this type supports contract X", you might look into using an interface, and calling a method on that interface after constructing the object. The interface would then have a known method (known to the code that needs to construct the instance) that you would call with known (known as in knowing how many, their types, as well as their meaning) parameters.

Just randomly (passing in 0 and null just to satisfy the signature is just as arbitrary as passing on 42 and some odd object) passing parameters to the constructor is not safe at all.

Example with interface:

object instance = Activator.CreateInstance(someTypeObject);
((ISomeInterface)instance).Initialize(your, specific, parameters, here);

What you might need is a IoC framework, so that you can say "whenever someone needs an instance of this class, here's how to construct it".

Look up:

4 Comments

I'm working on a custom serializer. And I need to get the instance when I'm deserializing. So I don't care if the created object has weird values, I just need the instance. And when I have the instance, I set the values with the other values saved.
You might not care, but the type will care, that's why it was created with no parameterless constructors. If the type has no constructors suitable for deserialization, the type is not designed for serialization. Change the type!
@LasseV.Karlsen - look at JerKimball answer below and also this method: msdn.microsoft.com/en-us/library/wcxyzt4d(v=vs.110).aspx
@Tohid I fail to see the connection. The question explicitly asked for something that automagically picked a constructor and just passed the default value for each parameter. The ability to pick a constructor by passing in values is not an answer, because that is what the OP explicitly asked to avoid. And if you read his (JerKimball)'s answer you'll see that he too understood this. My question is, what is it that you're seeing that we don't?
0

You state that your target class does not have a default (or "parameter-less") constructor. Put another way; your class's constructor contains "Dependencies." It cannot (and should not!) be constructed until those dependencies are met.

What you need is a Dependency Injection (or Inversion of Control) container. I recommend looking at Ninject, for excellent implementation which happens to be compatible with XNA.

Dependency Injection frameworks allow you to specify general 'rules,' such that the DI container can "fill in the blanks" when you encounter an object which requires parameters in the constructor. For example, an example of a DI rule for creating type Foo: "When you try to create an object of type Foo which has a constructor with parameters (int, object), pass these values to the constructor: (0, null)."

Spend a little bit of time glancing through some of the examples on the link provided, for an idea of how to achieve what you're asking using the Ninject library.

3 Comments

Comments give the downvotes context. Especially when the upvoted answer said the same thing... 3 minutes later.
My downvote: this does not answer the question or solve the problem the OP is asking about
@KierenJohnstone - The OP asked how to create an instance of an object which does not have a default/parameterless constructor. Put another way; the constructor in question contains dependencies. Thus, my answer - that he can use a "Dependency Injection" framework to instantiate such objects - is not only relevant, but considered a "best practice" among .NET developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.