简体   繁体   English

C#泛型铸造

[英]C# Generics Casting

Visual Studio 2008 has the ability to automatically create unit test stubs. Visual Studio 2008能够自动创建单元测试存根。 I have used this to create some basic unit tests, but I am confused by something: 我用它来创建一些基本的单元测试,但我对此感到困惑:

  private class bla : BaseStoreItem
     {
     //
     }

  /// <summary>
  ///A test for StoreData
  ///</summary>
  public void StoreDataTestHelper<T>() where T : BaseStoreItem
     {
     FileStore<T> target = new FileStore<T>(); // TODO: Initialize to an appropriate value

     BaseStoreItem data = new bla();

     target.StoreData(data);
     }

  [TestMethod()]
  public void StoreDataTest()
     {
     //Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
     //        "Please call StoreDataTestHelper<T>() with appropriate type parameters.");

     StoreDataTestHelper<bla>();
     }

Why do I get "Error: Cannot convert type 'StorageUnitTests.FileStoreTest.bla' to 'T'" when T is type "bla"? 当T为“bla”类型时,为什么会出现“错误:无法将类型'StorageUnitTests.FileStoreTest.bla'转换为'T'”?

I know "bla" is not a good function name, but its just an example. 我知道“bla”不是一个好的功能名称,但它只是一个例子。

why not that way? 为什么不那样? (It doesn't make much sence to create an instance of bla inside of StoreDataTestHelper if you have access to T) (如果你有权访问T,那么在StoreDataTestHelper中创建一个bla实例并不是很有意义)

 public void StoreDataTestHelper<T>() where T : BaseStoreItem, new()
 {
     FileStore<T> target = new FileStore<T>(); 

     T data = new T();

     target.StoreData(data);
 }

when T is type "bla" 当T是“bla”类型时

Your above condition holds true only for current case, but I can create a nother class 您的上述条件仅适用于当前案例,但我可以创建一个nother类

public class Bla2: BaseStoreItem {...

Then Bla2 <> bla ... , neither Bla2 is derived from bla, so if I try to use 然后Bla2 <> bla ...,Bla2都不是来自bla,所以如果我尝试使用

StoreDataTestHelper<Bla2>(); 

This is wrong, compiler is smart enough to understand that in this condition it will not work, computer languages are not like english, they are created to work exactly same in all conditions. 这是错误的,编译器足够聪明,理解在这种情况下它不起作用,计算机语言不像英语,它们被创建为在所有条件下完全相同。 And they are designed so that rules of language are correct in all cases. 它们的设计使得语言规则在所有情况下都是正确的。 If they differ you will have chaos finding where the error is. 如果它们不同,您将发现混乱,找到错误的位置。

因为,如果TDerivedStoreItem (继承BaseStoreItem ),则会通过存储BaseStoreItem来违反FileStore<T>BaseStoreItem

This makes sense. 这是有道理的。 By specifying T: BaseStoreItem , you have guaranteed that T will be a type the has BaseStoreItem as a base class, and NOT that it will necessarily be a BaseStoreItem . 通过指定T: BaseStoreItem ,您可以保证T将是一个具有BaseStoreItem作为基类的类型,而不是它必然是BaseStoreItem Therefore, if T is later set to be some type that derives from BaseStoreItem , your target.StoreData(data); 因此,如果稍后将T设置为从BaseStoreItem派生的某种类型,则使用target.StoreData(data); line would be performing an illegal operation. 线将执行非法操作。

Although in your case you only invoke StoreDataTestHelper with T set to bla , C#'s typechecker needs to make sure that the code for StoreDataTestHelper is type-safe in general. 虽然在你的情况下你只调用StoreDataTestHelper并将T设置为bla ,但C#的类型检查器需要确保StoreDataTestHelper的代码通常是类型安全的。 This is one of benefits to a strongly-typed language: it catches potential typing mistakes before you make them. 这是强类型语言的一个好处:它可以在你制作之前捕获潜在的输入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM