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;
}
}
}