The following code produced a conversion error on line
IName result = newtest.Add<testClass2<testClass>>(t2);
It seems like the code can not read the interface of the internal generic type. Am I using the wrong syntax of is this really a limitation?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
test newtest = new test();
testClass t1 = new testClass();
testClass2<testClass> t2 = new testClass2<testClass>();
IName result = newtest.Add<testClass2<testClass>>(t2);
Assert.AreEqual(result, t1);
}
public interface IName
{
public string Name { get; set; }
}
public interface ItestClass2<T> where T : IName
{
string ToString(T value);
}
public class testClass : IName
{
public string Name { get; set; }
}
public class testClass2<T> : ItestClass2<T> where T : IName
{
public string ToString(T value)
{
return value.GetType().FullName;
}
}
public class test
{
private Dictionary<string, ItestClass2<IName>> store = new Dictionary<string, ItestClass2<IName>>();
public T Add<T>(T value) where T : ItestClass2<IName>
{
this.store.Add(value.GetType().FullName, value);
return value;
}
}
}
}
The error message is:
Severity Code Description Project File Line Suppression State
Error CS0311 The type 'UnitTestProject1.UnitTest1.testClass2<UnitTestProject1.UnitTest1.testClass>' cannot be used as type parameter 'T' in the generic type or method 'UnitTest1.test.Add<T>(T)'. There is no implicit reference conversion from 'UnitTestProject1.UnitTest1.testClass2<UnitTestProject1.UnitTest1.testClass>' to 'UnitTestProject1.UnitTest1.ItestClass2<UnitTestProject1.UnitTest1.IName>'. UnitTestProject1 C:\Users\kevin\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs 17 Active