我遇到了泛型的问题,我相信可以通过协方差解决,但我不完全了解协方差的工作原理以及如何正确声明它。 假设我有以下接口和类:
public interface IOwnedObject<TUser>
where TUser : IBaseUser
{
string UserId { get; set; }
TUser User { get; set; }
}
public interface IBaseUser
{
string Id { get; set; }
}
public class User : IBaseUser
{
public string Id { get; set; }
}
public class SomeOwnedObject : IOwnedObject<User>
{
public string UserId { get; set; }
public User User { get; set; }
}
然后考虑以下代码:
var obj = new SomeOwnedObject();
if(obj is IOwnedObject<IBaseUser> o)
Console.WriteLine("Success"); // This never executes
以上if语句的计算结果不为true。但是,在IOwnedObject
中,TUser
只能是IBaseUser
。
以下代码的计算结果为true:
var obj = new SomeOwnedObject();
if(obj is IOwnedObject<User> o)
Console.WriteLine("Success"); // This executes
由于User
实现了IBaseUser
,因此从技术上讲IOwnedObject<IBaseUser>
不应是IOwnedObject<User>
的基类。是否可以在不引用具体实现User
的情况下使第一条语句评估为true?
答案 0 :(得分:5)
只需对界面进行一些修改,就可以得到第一个评估结果为true的语句。我们将要标记TUser
类型与out
关键字为协变,并且这样做我们必须删除属性设置器。
public interface IOwnedObject<out TUser> where TUser : IBaseUser
{
string UserId { get; set; }
TUser User { get; }
}
现在评估
var obj = new SomeOwnedObject();
if (obj is IOwnedObject<IBaseUser> o)
Console.WriteLine("Success");
将导致“成功”打印到控制台。
答案 1 :(得分:0)
或者执行@Jonathon的建议,或者执行以下操作。要使其协变,您需要使用不变类型,即IBaseUser。
| day | day_of_wk | day_of_mnth | day_of_qtr | day_of_yr | wk | wk_of_mnth | wk_of_yr | mnth | mnth_of_yr | qtr_of_yr | _yr | today | today_last_yr | yesterday | yesterday_last_yr | this_wk | this_wk_last_yr | this_mnth | this_mnth_last_yr | this_qtr | this_qtr_last_yr |
+---------+-----------+-------------+------------+-----------+---------+------------+----------+--------+------------+-----------+------+-------+-----------------+-----------+---------------------+-----------+---------------------+------------+----------------------+--------------+------------------------+
| 4/1/18 | 1 | 1 | 1 | 92 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/2/18 | 2 | 2 | 2 | 93 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/3/18 | 3 | 3 | 3 | 94 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/4/18 | 4 | 4 | 4 | 95 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/5/18 | 5 | 5 | 5 | 96 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/6/18 | 6 | 6 | 6 | 97 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/7/18 | 7 | 7 | 7 | 98 | 4/1/18 | 1 | 14 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/8/18 | 1 | 8 | 8 | 99 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/9/18 | 2 | 9 | 9 | 100 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/10/18 | 3 | 10 | 10 | 101 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/11/18 | 4 | 11 | 11 | 102 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | Yesterday Last Year | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/12/18 | 5 | 12 | 12 | 103 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | Today Last Year | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/13/18 | 6 | 13 | 13 | 104 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/14/18 | 7 | 14 | 14 | 105 | 4/8/18 | 2 | 15 | 4/1/18 | 4 | 2 | 2018 | | | | | | This Week Last Year | | This Month Last Year | | This Quarter Last Year |
| 4/15/18 | 1 | 15 | 15 | 106 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/16/18 | 2 | 16 | 16 | 107 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/17/18 | 3 | 17 | 17 | 108 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/18/18 | 4 | 18 | 18 | 109 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/19/18 | 5 | 19 | 19 | 110 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/20/18 | 6 | 20 | 20 | 111 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/21/18 | 7 | 21 | 21 | 112 | 4/15/18 | 3 | 16 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/22/18 | 1 | 22 | 22 | 113 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/23/18 | 2 | 23 | 23 | 114 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/24/18 | 3 | 24 | 24 | 115 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/25/18 | 4 | 25 | 25 | 116 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/26/18 | 5 | 26 | 26 | 117 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/27/18 | 6 | 27 | 27 | 118 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 4/28/18 | 7 | 28 | 28 | 119 | 4/22/18 | 4 | 17 | 4/1/18 | 4 | 2 | 2018 | | | | | | | | This Month Last Year | | This Quarter Last Year |
| 3/31/19 | 1 | 1 | 1 | 92 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/1/19 | 2 | 2 | 2 | 93 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/2/19 | 3 | 3 | 3 | 94 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/3/19 | 4 | 4 | 4 | 95 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/4/19 | 5 | 5 | 5 | 96 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/5/19 | 6 | 6 | 6 | 97 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/6/19 | 7 | 7 | 7 | 98 | 3/31/19 | 1 | 14 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/7/19 | 1 | 8 | 8 | 99 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | | | This Week | | This Month | | This Quarter | |
| 4/8/19 | 2 | 9 | 9 | 100 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | | | This Week | | This Month | | This Quarter | |
| 4/9/19 | 3 | 10 | 10 | 101 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | | | This Week | | This Month | | This Quarter | |
| 4/10/19 | 4 | 11 | 11 | 102 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | Yesterday | | This Week | | This Month | | This Quarter | |
| 4/11/19 | 5 | 12 | 12 | 103 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | Today | | | | This Week | | This Month | | This Quarter | |
| 4/12/19 | 6 | 13 | 13 | 104 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | | | This Week | | This Month | | This Quarter | |
| 4/13/19 | 7 | 14 | 14 | 105 | 4/7/19 | 2 | 15 | 4/1/19 | 4 | 2 | 2019 | | | | | This Week | | This Month | | This Quarter | |
| 4/14/19 | 1 | 15 | 15 | 106 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/15/19 | 2 | 16 | 16 | 107 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/16/19 | 3 | 17 | 17 | 108 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/17/19 | 4 | 18 | 18 | 109 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/18/19 | 5 | 19 | 19 | 110 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/19/19 | 6 | 20 | 20 | 111 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/20/19 | 7 | 21 | 21 | 112 | 4/14/19 | 3 | 16 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/21/19 | 1 | 22 | 22 | 113 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/22/19 | 2 | 23 | 23 | 114 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/23/19 | 3 | 24 | 24 | 115 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/24/19 | 4 | 25 | 25 | 116 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/25/19 | 5 | 26 | 26 | 117 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/26/19 | 6 | 27 | 27 | 118 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |
| 4/27/19 | 7 | 28 | 28 | 119 | 4/21/19 | 4 | 17 | 4/1/19 | 4 | 2 | 2019 | | | | | | | This Month | | This Quarter | |