0

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);
        }
    }
    
}
RSSM
  • 669
  • 7
  • 19

2 Answers2

0

The problem in your Job1 class:

public class Job1<T> : IJob<RealThing>

Type parameter in Job1 class (T) not passing to IJob, so thats means, that T is not applied to IThing.

Rework your class to

public class Job1<T> : IJob<T>

or

public class Job1 : IJob<RealThing>
Dmitriy Korolev
  • 288
  • 1
  • 10
0

When you Create an instance of an IJob<T1>, and another instance of IJob<T2>, those are not of the same type. So you cannot assign a IJob<RealThing> to a IJob<IThing>

You could rework it as follows. But it seems as if the generic arguments are just making it complicated.

     namespace ConsoleApp28
{
    using System.Collections.Generic;

    public class Program
    {
        public static void Main()
        {
            var service = new Service<IJob<RealThing>, RealThing>();
            var job = new Job1();
            var rt = new RealThing();

            service.job = new Job1();
            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 : IJob<RealThing>
        {
            public string GetSomething(RealThing athing)
            {
                return "Job1.RealThing";
            }
        }

        public class Service<TJob, T>
            where TJob : IJob<T>
            where T : IThing
        {
            public TJob job { get; set; }
            public IEnumerable<IThing> List { get; set; }

            public string GetSomething(T aThing)
            {
                return job.GetSomething(aThing);
            }
        }
    }
}

Could an IJob not simply consume an IThing? Does it have to be a T that is an IThing?

Quantifeye
  • 261
  • 2
  • 10