C# Generics Casting

Posted by Nippysaurus on Stack Overflow See other posts from Stack Overflow or by Nippysaurus
Published on 2010-03-26T06:58:30Z Indexed on 2010/03/26 7:03 UTC
Read the original article Hit count: 231

Filed under:
|
|

Visual sutdio 2008 has the ability to automatically create unit test stubs. 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"?

I know "bla" is not a good function name, but its just an example.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET