1

I have an object that is of type IEnumerable<Product>

// var objEnum is of type IEnumerable<Product>

I have a function with parameter IEnumerable<IProduct<object>>

void MethodName (IEnumerable<IProduct<object>> obj) { return; } 

I want to call the function:

MethodName( objEnum ) 

but it seems I need to cast objEnum to IEnumerable<IProduct<object>> somehow, am I right in needing to cast it? Is this possible? What methods can I apply to objEnum to make pass correctly?

If NotProduct is another type defined by

class NotProduct 
{
}

and IProduct is defined by

interface IProduct<T> 
{ 
    void IProductMethod(T obj);
}

my Product class is defined as follows:

class Product : IProduct<NotProduct>
{ 
    void IProductMethod(NotProduct obj) { return;}
}

I am using my method as follows:

IEnumerable<Product> productList = new List<Product>(); 
MethodName(productList);
// return nothing, do nothing, just don't throw an exception

EDIT: FULL CODE

using System;

using System.Collections.Generic;



namespace ConsoleApp1

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Hello World!");



            IEnumerable<Product> productList = new List<Product>();

            NotProduct notProduct = new NotProduct();

            MyMethods.MethodName(productList, notProduct);



            IEnumerable<AnotherProduct> anotherProductList = new List<AnotherProduct>();

            NotAnotherProduct notAnotherProduct = new NotAnotherProduct();

            MyMethods.MethodName(anotherProductList, notAnotherProduct);



            System.Diagnostics.Debug.WriteLine("Program ran sucessfully");

        }



    }



    public static class MyMethods

    {

        public static void MethodName(IEnumerable<IProduct<object>> collection, object obj)

        {

            foreach (IProduct<object> aProduct in collection)

            {

                if (obj is NotProduct && aProduct is IProduct<NotProduct>)

                {

                    aProduct.IProductMethod(obj);

                }

                else if (obj is NotAnotherProduct && aProduct is IProduct<NotAnotherProduct>)

                {

                    aProduct.IProductMethod(obj);

                }

            }

        }



    }



    public interface IProduct<T>

    {

        void IProductMethod(T t);

    }



    public class NotAnotherProduct

    {



    }



    public class AnotherProduct : IProduct<NotAnotherProduct>

    {

        public void IProductMethod(NotAnotherProduct t)

        {

            return;

        }

    }



    public class NotProduct

    {

    }



    public class Product : IProduct<NotProduct>

    {

        public void IProductMethod(NotProduct t)

        {

            return;

        }

    }

}
John
  • 85
  • 7
  • Did you get any issue while calling method that accepts IEnumerable> obj with objEnum – Ramanathan Ganesan Aug 17 '18 at 23:51
  • 1
    Why you mentioned NotProduct class? my understanding is you are trying to pass IEnumerable object as parameter to a method that accepts IEnumerable>. Correct me if my understanding is wrong – Ramanathan Ganesan Aug 17 '18 at 23:54
  • @RamanathanGanesan That is incorrect. Although subtile, I am trying to pass IEnumerable to IEnumerable< **I** Product>. I mention the NotProduct class because I want Product to implement the interface with a different generic type – John Aug 18 '18 at 00:00
  • @RamanathanGanesan Cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable>' – John Aug 18 '18 at 00:02
  • FYI, method names should be `PascalCase` in C# (referring to `methodName`). And the `IProductMethod` has to be public in the `Product` class in order to fulfil the interface contract. – Rufus L Aug 18 '18 at 00:34
  • IProductMethod doesn't if it's only getting called external as a resource. I think your issue is: "void methodName (IEnumerable> obj) { return; } "... without seeing how your calling it, I couldn't say... but shouldn't the "object" be NotProduct – Monofuse Aug 18 '18 at 01:17
  • Not necessarily. I want to be able make another class, call it NewProduct inherit IProduct. Then pass IEnumerable as IEnumerable> into **M**ethodName( ). It really doesn't matter what I'm doing in the function. It's more of like, how do I pass it correctly? I edited my comment to show how I am calling it. – John Aug 19 '18 at 19:59
  • It sound like you've over architected something. Why you need a generic interface? for `void IProductMethod(T obj);` Why can't you just have `void IProductMethod(IAnotherInterface obj);` – johnny 5 Aug 19 '18 at 20:17
  • I could add IAnotherInterface which I could use to cast to IEnumerable> instead of IEnumerable>. But then I have to implement IAnotherInterface to the 25 'T' classes that IProduct implements! Is this what you meant by using IProductMethod(IAnotherInterface obj) – John Aug 19 '18 at 20:22

0 Answers0