0

I need to have something like this. So a class that implements this Interface needs to get a property with the same type as the class. Is this even possible, ifso how? I'm using .net 6.

public interface IProperty
{
    public typeof(this) parameter { get; } // doesn't work, can't use typeof() 
}   
public class clsResourceProperty : IProperty
{
    public clsResourceProperty  parameter { get; }
} 
public class clsInterfaceProperty : IProperty
{
    public clsInterfaceProperty  parameter { get; }
}

I know how to use generic interfaces, and with the second example it works, but clsResourcePropertyGeneric: IPropertyGeneric looks strange. And doens't work for the application i need.

public interface IPropertyGeneric<T>
{
    public T parameter { get; }
}
public class clsResourcePropertyGeneric: IPropertyGeneric<clsResourcePropertyGeneric>
{
    public clsResourcePropertyGeneric parameter { get; }
}
public class clsInterfacePropertyGeneric: IPropertyGeneric<clsInterfacePropertyGeneric>
{
    public clsInterfacePropertyGeneric parameter { get; }
}

In the application i need, i need to have a class containing a list of this interface. So something like this:

public class clsState 
{
    public List<IProperty> data {get; private set;}
    public clsState(List<IProperty> data)
    {
        this.data = data;
    }
    public void logic()
    {
        foreach(var d in data) 
        {
            //do something with d.parameters
        }
    }
}

But this doesn't work with the generic interface. I need to make a class containing a list of this interface, where i define the generic type T. But this list can't contain ALL classes which implement this interface

public class clsState<T> 
// need to add T here to use it in the list, but the list needs to contain ALL implementing class types, not only 1
{
    public List<IProperty<T>> data {get; private set;}
    public clsState(List<IProperty<T>> data)
    {
        this.data = data;
    }
    public void logic()
    {
        foreach(var d in data) 
        {
            //do something with d.parameters
        }
    }
}

I found this link, but this is from 7 years ago, so maybe there is some evolution in this aspect?

avalboom
  • 1
  • 1
  • 4
    Yeah `C : I2` looks a little weird but it does exactly what you want! – phuzi Dec 07 '22 at 10:02
  • 3
    _Names make all the difference._ `public interface IDescendable { T Parent { get; } } public class Folder: IDescendable { public Folder Parent { get; } }` –  Dec 07 '22 at 10:12
  • There is no common interface between `IPropertyGeneric` and `IPropertyGeneric`. Suppose there was and you had a list of that type. How would you know what type `list[0].parameter` was? What could you do with it? – D Stanley Dec 07 '22 at 22:28

1 Answers1

0

You can use the interface as your property type, as in:

public interface IProperty
{
    public IProperty parameter { get; } 
}

public class clsResourceProperty : IProperty
{
    public IProperty parameter { get; }
}
public class clsInterfaceProperty : IProperty
{
    public IProperty parameter { get; }
}

As for having a collection of the interfaces, it is possible to collect all of the classes for a particular type or interface. This is from a piece of code in one of my libraries. It doesn't do exactly what you're after, but it might be a step towards your final solution.

private static Type[] strategyTypes;

private readonly static Type[] obsoleteTypes = new Type[]
{
};

static StrategyRepository()
{
    strategyTypes = Assembly.GetExecutingAssembly().GetTypes()
        .Where(t => t.BaseType == typeof(Strategy))
        .Except(obsoleteTypes)
        .ToArray();
}

This question might be a more direct answer to that part of your question: Getting all types that implement an interface

Vic F
  • 1,143
  • 1
  • 11
  • 26