Example 1 (does not compile):
void Main()
{
var c = new C<D>();
c.M.F();
}
class C<T>
{
T _m = null;
public T M { get {
if(_m == null) _m = new T();
return _m;
} }
}
class D
{
public void F() { Console.WriteLine ("i was created"); }
}
Result:
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
Example 2 (works):
void Main()
{
var c = new C<D>();
c.M.F();
}
class C<T>
{
Lazy<T> _m = new Lazy<T>();
public T M { get { return _m.Value; } }
}
class D
{
public void F() { Console.WriteLine ("i was created"); }
}
Result:
i was created
If you delve into the source code, you'll see that Lazy<T> ultimately uses Activator:
return new Lazy<T>.Boxed((T)Activator.CreateInstance(typeof(T)));
This is just a shortcut for using reflection. Since it's not instantiating the type via the actual generic type argument (new T()) but rather invoking the constructor through reflection, no where T : new() constraint is needed.
Related
Here's some code to give the idea.
public class C<T> { }
public class X {
void M() {
var V = new { W = 1 };
var X = new C<V>(); // illegal
}
}
So V is a variable of an anonymous type and I would like to instantiate class C with V as its argument. This requires a type argument, which I cannot supply.
This questions is similar but the answer is not much help in my case: Why can't I instantiate a generic class inferring types from anonymous objects?
The question behind the question is that I'm trying to do what IEnumerable can do.
Should have made it clear: I would really prefer not to do this by manipulating object or Type because you lose the benefits of strong typing and Intellisense.
For anyone interested, the project that needs this is described here: http://www.andl.org/2016/07/andl-net-making-progress/.
You can use type inference if you have a factory method:
public class C<T>
{
public C(T t)
{
// ...
}
}
public static class Factory
{
public static C<T> Create<T>(T t)
{
return new C<T>(t);
}
}
public class Thing
{
void Foo()
{
var x = new { y = "z" };
//var thing = new C(x); - doesn't work, you need to specify the generic parameter
var thing = Factory.Create(x); // T is inferred here
}
}
You can't do that since V is instance of anonymous type, not a type name itself.
You can create this type dynamically (assuming parameterless constructor in C<>):
var X = typeof (C<>)
.MakeGenericType(V.GetType())
.GetConstructor(Type.EmptyTypes)
.Invoke(new object[0]);
You need a type, you can use pass object as a type.
Sample code:
public class C<T>
{
public T _t { get; set; }
public C(T t)
{
_t = t;
}
public void TestMethod()
{
Console.WriteLine(_t.ToString());
}
}
public class X
{
public void M()
{
var V = new { W = 1 };
var X = new C<object>(V); // everything is an object.
X.TestMethod();
}
}
ClassA, ClassB, ClassC and ClassD are all implementing IMyClass interface.
myObj is an instance of one of the classes.
private void setObj<T>()
{
myObj = mycollection.Single(w => w is T);
}
public void Switch()
{
if(myObj is ClassA)
{
setObj<ClassA>();
}
else if(myObj is ClassB)
{
setObj<ClassB>();
}
else if(myObj is ClassC)
{
setObj<ClassC>();
}
else if(myObj is ClassD)
{
setObj<ClassD>();
}
}
How can we refactor the Switch method, so that I have something like this:
public void Switch()
{
// How can we know from `myObj`, which class it is and rewrite
// the whole Switch method like this
// X = `ClassA`, `ClassB`, `ClassC` or `ClassD`
setObj<X>();
}
You cannot pass a generics type parameter as a variable in C#. However, you can get the type via reflection (myObj.GetType()) and pass that as a function parameter from your Switch() function to your setObj() function, which in turn can be compared in your lambda:
private void setObj(Type type)
{
myObj = Objects.Single(o => o.GetType() == type);
}
public void Switch()
{
Type setToThisType = myObj.GetType();
setObj(setToThisType);
}
make Switch a generic method too that accepts an object of type T
public void Switch<T>(T obj) where T : IMyClass
{
setObj<T>();
}
The where T : IMyClass statement ensures that you can only call Switch where obj is an instance of a class implementing IMyClass
void Example()
{
ClassA objA = new ClassA();
Switch(objA); //OK;
ClassX objX = new ClassX();
Switch(objX); //compile-time error since ClassX doesn't implement IMyClass
}
EDIT: after reading the title, I think you would need to have the parameter T obj in the Switch method.
Try using typeof(ClassA)
public void TypeTest(Type t)
{
if(t.Equals(typeof(ClassA))){
}
}
i'm trying to do fancy stuff like this:
i have following code:
public interface IMyInterface
{
void Method1();
}
public interface IClassFactory
{
object GetObject();
}
public interface IGenericClassFactory<T> where T: IMyInterface
{
T GetObject();
}
public class MyClass : IMyInterface
{
public void Method1()
{
Console.WriteLine("Medota 1");
}
}
public class MyFactory : IClassFactory
{
public object GetObject()
{
return new MyClass();
}
}
public class MyGenericFactory<T> : IGenericClassFactory<T> where T : IMyInterface, new()
{
public T GetObject()
{
// T t = new T();
//return t;
//var ctor = typeof(T).GetConstructor(new Type[0]);//1] { typeof(int) });
//if (ctor != null)
//{
// return (T)ctor.Invoke(new object[0]); // new object[1] { 5});
// //return Activator.CreateInstance<T>(); //to samo co wyzej tylko nie jest bezpieczne
//}
//throw new InvalidOperationException("T nie posiada domyślnego konstruktora");
// return Activator.CreateInstance<T>(); //bez parametrów
// return (T)Activator.CreateInstance(typeof(T), 5, "EOG", new object()); // z parametrami
return new T();
}
}
static void Main(string[] args)
{
IClassFactory factory;
factory = new MyFactory();
IGenericClassFactory<IMyInterface> genFactory;
genFactory = new MyGenericFactory<MyClass>(); //Do not compile!
MyClass obj = genFactory.GetObject() as MyClass;
obj.Method1();
Console.ReadKey();
}
I can do this like:
IGenericClassFactory<IMyInterface> genFactory;
genFactory = new MyGenericFactory<MyClass>();
//so i can chose object to create
but i think it is pointless because i want to have Factory of more then one object.
Can u help me?
Thx in advance
You should not make your factory class generic but the method GetObject should be generic:
public T GetObject<T>() where T: IMyInterface, new()
Then:
static void Main(string[] args)
{
var factory = new MyFactory();
var obj = factory.GetObject<MyClass>();
obj.Method1();
Console.ReadKey();
}
So all in all you should get rid of your generic code and simply modify your MyFactory class
public class MyFactory : IClassFactory
{
public T GetObject<T>()
{
//TODO - get object of T type and return it
return new T();
}
}
By the way - I am not sure what is the purpose of having this generic implementation? Does it make any sense from the perspective of the usage of Factory pattern?
I have hierarchy of classes:
class A{}
class B: A {}
class C:B {}
is it possible to implement method in class A and it would be inherited by derived classes B and C and so on and that method should return value of class type?
A val = A.method(); (val is A)
B val = B.method(); (val is B)
C val = C.method(); (val is C)
And I don't want use of generics in call of this method, ie:
C val = C.method<C>();
Guys, excuse me, one elaboration, this method should be static.
I don't want to use generic in method istelf, because it forces to point type that method should return, whereas method should return type of its class.
class A
{
Method<T>()
{
T result;
return result;
}
}
If I have such method I can change return type:
D result = A.Method<D>();
but I wanted it to return value of type A;
No, that is not possible.
To call the method like that it would have to be static, and static methods are not inherited.
Using B.method() to call a static method in A is the same as using A.method(). The compiler just uses the type to determine where the method is, but it's impossible for the method to know if it was called using the A or B type.
Use an extension method:
class Program
{
static void Main(string[] args)
{
B x = new B();
x.Method();
}
}
public static class Ext
{
public static T Method<T>(this T obj)
where T : A,new()
{
return new T();
}
}
public class A
{
}
public class B : A
{
}
Or a variation thereof. Note that you must have some public member capable of creating an instance of the specified type. To expound, the compiler 'guesses' the value of the type parameter. The method is still generic, but generic syntax is nowhere to be seen when the method is called (usually).
Using some design patterns from C++ makes this easier:
class A
{
protected virtual A method_impl() { return new A(); }
public A method() { return method_impl(); }
}
class B : A
{
protected override A method_impl() { return new B(); }
public new B method() { return (B)method_impl(); }
}
class C : B
{
protected override A method_impl() { return new C(); }
public new C method() { return (C)method_impl(); }
}
Of course, this exact problem never arises in C++, which allows covariant return types for overrides.
Another way, using IoC pattern:
class A
{
protected virtual void method_impl(A a) { a.initialize(); }
public A method() { A result = new A(); method_impl(result); return result; }
}
class B : A
{
public new B method() { B result = new B(); method_impl(result); return result; }
}
class C : B
{
public new C method() { C result = new C(); method_impl(result); return result; }
}
I have the following C# test code:
class MyItem
{
MyItem( int a ) {}
}
class MyContainer< T >
where T : MyItem, new()
{
public void CreateItem()
{
T oItem = new T( 10 );
}
}
Visual Studio can't compile it, the error is at line where 'new' is used:
'T': cannot provide arguments when creating an instance of a variable type
Is it possible in C# to create an object of generic type with non-parameterless constructor? It's no problem to do such thing in C++ templates, so i'm very curious why i can't do same thing in C#. Maybe some additional 'where' is required or syntax is different?
C#, and VB.Net for that matter, do not support the notion of constraining a generic to have a constructor with specific parameters. It only supports constraining to have an empty constructor.
One work around is to have the caller pass in a factory lambda to create the value. For instance
public void CreateItem(Func<int,T> del) {
T oItem = del(10);
}
Call site
CreateItem(x => new SomeClass(x));
It can be done with reflection:
public void CreateItem()
{
int constructorparm1 = 10;
T oItem = Activator.CreateInstance(typeof(T), constructorparm1) as T;
}
But there is no generic constraint to ensure that T implements the desired constructor, so I wouldn't advise doing this unless you are careful to declare that constructor in every type that implements the interface.
There's no such generic constraint, so it's not possible directly (this is a CLR limitation). If you want this, you have to provide a factory class (which has a parameterless constructor), and pass it as a second generic type parameter.
IMO, the best approach here is an initialize method, i.e.
interface ISomeInterface {
void Init(int i);
}
class Foo : ISomeInterface {
void ISomeInterface.Init(int i) { /* ... */ }
}
static class Program {
static T Create<T>(int i) where T : class, ISomeInterface, new() {
T t = new T();
t.Init(i);
return t;
}
static void Main() {
Foo foo = Create<Foo>(123);
}
}
However, you can do what you want with Expression (but without compile-time support):
using System;
using System.Linq.Expressions;
class Foo {
public Foo(int i) { /* ... */ }
}
static class Program {
static T Create<T>(int i) {
return CtorCache<T>.Create(i);
}
static class CtorCache<T> {
static Func<int, T> ctor;
public static T Create(int i) {
if (ctor == null) ctor = CreateCtor();
return ctor(i);
}
static Func<int, T> CreateCtor() {
var param = Expression.Parameter(typeof(int), "i");
var ci = typeof(T).GetConstructor(new[] {typeof(int)});
if(ci == null) throw new InvalidOperationException("No such ctor");
var body = Expression.New(ci, param);
return Expression.Lambda<Func<int, T>>(body, param).Compile();
}
}
static void Main() {
Foo foo = Create<Foo>(123);
}
}
Note that this caches and reuses the delegate for performance.
One pattern I use is to have the constrained class implement an interface which defines an Init method with the appropriate signature:
interface IMyItem
{
void Init(int a);
}
class MyItem : IMyItem
{
MyItem() {}
void Init(int a) { }
}
class MyContainer< T >
where T : MyItem, IMyItem, new()
{
public void CreateItem()
{
T oItem = new T();
oItem.Init( 10 );
}
}