Typ musi być typem odniesienia, aby użyć go jako parametru "T" w typie ogólnym lub metodzie
Zagłębiam się w generyki i teraz mam sytuację, w której potrzebuję pomocy. Dostaję błąd kompilacji na' pochodnej ' klasie poniżej, jak pokazano w tytule tematu. Widzę wiele innych postów podobnych do tego, ale nie widzę związku. Czy ktoś może mi powiedzieć, jak to rozwiązać?
using System;
using System.Collections.Generic;
namespace Example
{
public class ViewContext
{
ViewContext() { }
}
public interface IModel
{
}
public interface IView<T> where T : IModel
{
ViewContext ViewContext { get; set; }
}
public class SomeModel : IModel
{
public SomeModel() { }
public int ID { get; set; }
}
public class Base<T> where T : IModel
{
public Base(IView<T> view)
{
}
}
public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
{
public Derived(IView<SomeModel> view)
: base(view)
{
SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
Service<SomeModel> s = new Service<SomeModel>();
s.Work(m);
}
}
public class Service<SomeModel> where SomeModel : IModel
{
public Service()
{
}
public void Work(SomeModel m)
{
}
}
}
215
3 answers
Nie mogę repro, ale podejrzewam że w Twoim kodzie jest gdzieś ograniczenie, które T : class
- Musisz to propagować, aby kompilator był szczęśliwy, na przykład (trudno powiedzieć na pewno bez przykładu repro):
public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
^^^^^
see this bit
484
Author: Marc Gravell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-23 08:27:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-23 08:27:06
Dostajesz ten błąd, jeśli ograniczyłeś T
do bycia class
48
Author: thekip,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-23 08:25:25
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-23 08:25:25
Jeśli umieścisz ograniczenia na klasie lub metodzie generycznej, każda inna klasa lub metoda generyczna, która jej używa, musi mieć "co najmniej" te ograniczenia.
9
Author: Guish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-07 14:48:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-07 14:48:41