简体   繁体   中英

Collection of Generic Interfaces

I have a generic interface like this:

public interface IHardwareProperty<T>{
bool Read();
bool Write();
}

Which is "passed through" an abstract base class:

public abstract class HardwareProperty<T>:IHardwareProperty<T>{
//Paritally implements IHardwareProperty (NOT SHOWN), and passes Read and Write through
//as abstract members.
    public abstract bool Read();
    public abstract bool Write();
}

and is completed in several inheriting classes that use different generic arguments

CompletePropertyA:IHardwareProperty<int>
{
    //Implements Read() & Write() here
}

CompletePropertyBL:IHardwareProperty<bool>
{
    //Implements Read() & Write() here
}

I'd Like to store a bunch of completed properties of different types in the same collection. Is there a way to do this without resorting to having a collection of object s?

You need to use a type that is supported by all of these. You could do this by making your IHardwareProperty<T> interface non-generic:

public interface IHardwareProperty
{
    bool Read();
    bool Write();
}

Since none of the methods in your interface use T , this is perfectly appropriate. The only reason to make the interface generic would be if you're using the generic type within the interface methods or properties.

Note that your base class can still be generic if this is required or desired for the implementation details:

public abstract class HardwareProperty<T> : IHardwareProperty
{
   //...

不幸的是没有,因为每个具有不同类型参数的泛型都被视为完全不同的类型。

typeof(List<int>) != typeof(List<long>)

Posting this for posterity: My problem was almost identical to this, but with a slight tweak that my interface did utilize the generic type.

I have multiple classes which implement a generic interface. The goal is to have multiple <Int/Bool/DateTime>Property classes which each contain a string ( _value ) and a getValue() function which, when called, converts the string _value to a different type, depending on the implementation of the interface.

public interface IProperty<T>
{
    string _value { get; set; }
    T getValue();
};

public class BooleanProperty : IProperty<bool>
{
    public string _value { get; set; }
    public bool getValue()
    {
        // Business logic for "yes" => true
        return false;
    }
}

public class DateTimeProperty : IProperty<DateTime>
{
    public string _value { get; set; }
    public DateTime getValue()
    {
        // Business logic for "Jan 1 1900" => a DateTime object
        return DateTime.Now;
    }
}

And then I wanted to be able to add multiple of these objects into a single container to then call getValue() on each one and it would return a bool, or DateTime or whatever depending on its type.

I thought I'd be able to do he following:

List<IProperty> _myProperties = new List<IProperty>();

But that produces the error:

Using the generic type 'IProperty<T>' requires 1 type arguments

But I don't yet know what the types of this list will be, so I tried adding a type of <object>

List<IProperty<object>> _myProperties = new List<IProperty<object>>();

And that compiles. Then I can add items to the collection, but I need to cast them to IProperty<object> which is ugly, and to be quite honest, I'm not exactly sure what that is doing under the hood.

BooleanProperty boolProp = new BooleanProperty();
// boolProp.getValue() => returns a bool
DateTimeProperty dateTimeProp = new DateTimeProperty();
// dateTimeProp.getValue(); => returns a DateTime
_myProperties.Add((IProperty<object>)boolProp);
_myProperties.Add((IProperty<object>)dateTimeProp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM