I am getting the following error for this line service.job = new Job1<RealThing>();
Cannot implicitly convert type 'Program.Job1<Program.Thing1>' to 'Program.IJob<Program.IThing>'. An explicit conversion exists (are you missing a cast?)
I am struggling to get this to work and in understanding why I get this error here, but not the same error for service.List = new List
();
using System; using System.Collections.Generic; public class Program { public static void Main() { Service service = new Service(); IJob<RealThing> job = new Job1<RealThing>(); RealThing rt = new RealThing(); service.job = new Job1<RealThing>(); service.List = new List<RealThing>(); service.GetSomething(rt); //Console.WriteLine(service.GetSomething(rt)); } public interface IThing { } public class RealThing : IThing { } public interface IJob<in T> where T : IThing { string GetSomething(T aThing); } public class Job1<T> : IJob<RealThing> { public string GetSomething(RealThing athing) { return "Job1.RealThing"; } } public class Service { public IJob<IThing> job { get; set; } public IEnumerable<IThing> List { get; set; } public string GetSomething(IThing aThing) { return job.GetSomething(aThing); } } }
https://stackoverflow.com/questions/67248665/why-cant-i-assign-this-property-of-custom-type April 25, 2021 at 08:26AM
没有评论:
发表评论