This question already has answers here:
Create an instance of a class from a string
(8 answers)
Closed 8 years ago.
In a namespace I have an arbitrary number of classes fulfilling an interface IModel. Given the name of a class as a string, I want to instantiate that class and store the resulting object in a variable of type IModel.
As I have no experience in reflection, I did not figure out how to do it.
You don't need reflection here - use Activator
IModel model = (IModel)Activator.CreateInstance(Type.GetType(typeName));
You could have a look at Activator.CreateInstance.
There are many ways to do this. I do the following:
Type t = Type.GetType("<name of class>");
IModel m = (IModel)Activator.CreateInstance(t); // assuming constructor has no parameters
Related
This question already has answers here:
Is there a generic constructor with parameter constraint in C#?
(9 answers)
Closed 1 year ago.
I am trying to do something like this:
where DataTableLoader2 is a generic helper class
public static class DataTableLoader2 <T> where T : class, new (StringComparer)
Thanks for any help offered....
K
No, it can't. The purpose of new() is simply to enforce that T must have a default, parameterless constructor. If you want to enforce a generic having some common initialization then you should do it through either a base class, or an interface.
This question already has answers here:
C# use System.Type as Generic parameter
(3 answers)
Closed 4 years ago.
PROBLEM DESCRIPTION
I have an interface definition IFoo<TBar> : IFoo and a method CreateFooUsingBarType(Type barType). I need the method to resolve an IFoo<TBar> using a dependency injection tool given a specified System.Type instance that defines TBar. Don't ask how I ended up here. I am stuck within these boundaries.
EXAMPLE
public IFoo CreateFooUsingBarType(Type barType)
{
var iocScope = GetScope();
// TODO: Create a System.Type for IFoo<TBar>
// where TBar is barType. Blarghity blargh.
var fooType = (Type)null;
return iocScope.Resolve(fooType);
}
I've tried mucking around with TypeBuilder but I'm getting the sense that it's overkill for this. Does .NET expose a different API for achieving this?
Thanks in advance.
You can use the MakeGenericType method:
var fooType = typeof(IFoo<>).MakeGenericType(barType);
This question already has answers here:
Redundant to inherit from Object in C#?
(7 answers)
Closed 7 years ago.
In this MSDN example, the class explicitly inherits from Object:
class Point: Object {
// ...
}
Is explicitly inheriting from Object ever necessary? That is, is it not equivalent to the following?
class Point: Object {
// ...
}
No, it is implicitly inherited. This is why every class in C# has a .ToString() and .Equals().
https://msdn.microsoft.com/en-us/library/vstudio/system.object%28v=vs.100%29.aspx
Object class: "This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."
This type of inheritance where everything is derived from a single class is called a "unified type system"
This question already has answers here:
How do I check if a type is a subtype OR the type of an object?
(5 answers)
Closed 9 years ago.
Suppose I have a class that looks like this:
class Derived : // some inheritance stuff here
{
}
I want to check something like this in my code:
Derived is SomeType;
But looks like is operator need Derived to be variable of type Dervied, not Derived itself.
I don't want to create an object of type Derived.
How can I make sure Derived inherits SomeType without instantiating it?
P.S. If it helps, I want something like what where keyword does with generics.
EDIT:
Similar to this answer, but it's checking an object. I want to check the class itself.
To check for assignability, you can use the Type.IsAssignableFrom method:
typeof(SomeType).IsAssignableFrom(typeof(Derived))
This will work as you expect for type-equality, inheritance-relationships and interface-implementations but not when you are looking for 'assignability' across explicit / implicit conversion operators.
To check for strict inheritance, you can use Type.IsSubclassOf:
typeof(Derived).IsSubclassOf(typeof(SomeType))
Try this
typeof(IFoo).IsAssignableFrom(typeof(BarClass));
This will tell you whether BarClass(Derived) implements IFoo(SomeType) or not
This question already has answers here:
Create an instance of a class from a string
(8 answers)
Closed 9 years ago.
I know the type of object (let's say IAnimal) I need to instantiate, and the name (lets say Tiger). How do I write the code to instantiate Tiger, given that the variable that knows the object name is a string. I'm likely missing something simple here, but am currently stuck on this.
Update: I meant Class Tiger : IAnimal, changed above to reflect that.
Using the Activator.CreateInstance method
example:
// the string name must be fully qualified for GetType to work
string objName = "TestCreateInstance.MyObject";
IProcess txObject = (IProcess)Activator.CreateInstance(Type.GetType(objName));
or
object o = Activator.CreateInstance("Assem1.dll", "Friendly.Greeting");
See also: Reflection Examples C#
Use reflection to instantiate an object of a class by its type name.
object o = Activator.CreateInstance(Type.GetType("Tiger"));
Note that you cannot instantiate interfaces (judging by your naming), as they are merely a contract that defines what a specific class should implement.
I am not sure I fully understand the question. However, assuming that ITiger is actually a concrete class and not an interface (as the I* would suggest).
You can use reflection to create and instance of a type from a string.
Something like:
ITiger myTiger = Activator.CreateInstance("ITiger") as ITiger;
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
Is that what you are asking?