简体   繁体   中英

How to Compose two objects at runtime in C# only if the first object implements interfaces required by the second?

Say I have two classes. I want to compose class A into class B.

However, for this to be valid, class B must implement a specific set of interfaces required by class A.

How could I associate class A with the list of interfaces it requires class B to implement?

I want to do this so that at run time, some type of controller can decide whether or not to add one object to the other. It will only add a to b if b implements all of the interfaces required by a.

I hope this makes sense! Thanks!

As far as I am concerned you more or less require the following code. I wrote lots of comments into the code so instead of trying to explain in detail I will let the code do the talking

// I defined a couple of interfaces for test purposes
public interface IA { }
public interface IB : IA { }
public interface IC : IB { }

// ObjectB implements IB thereforce IA as well
public class ObjectB : IB { }


public class ObjectA {

    /// <summary>
    /// Interfaces that Object B must implement
    /// </summary>
    private readonly HashSet<Type> _interfacesObjectBMustImplement = new HashSet<Type>();

    /// <summary>
    /// Adds an interface requirement for object B to implement
    /// </summary>
    /// <param name="requiredInterface">Type of required interface</param>
    public void AddrequiredInterface(Type requiredInterface) {
        var isInterface = requiredInterface.IsInterface;
        if (isInterface == false) {
            throw new Exception($"{requiredInterface.Name} is not an interface");
        }

        var added = _interfacesObjectBMustImplement.Add(requiredInterface);
        if (added == false) {
            throw new Exception($"{requiredInterface.Name} is aready added");
        }
    }

    /// <summary>
    /// Clear the required interface requirements
    /// </summary>
    public void ClearRequiredInterfaces() {
        _interfacesObjectBMustImplement.Clear();
    }

    /// <summary>
    /// Checks whether object A can be composed into <see cref="T" />
    /// </summary>
    /// <typeparam name="T">Type of target object</typeparam>
    /// <param name="targetObjectB">Target object instance</param>
    /// <returns></returns>
    public bool CanBeComsposedInto<T>(T targetObjectB) {
        var allInterfaces = typeof(T).GetInterfaces();
        return _interfacesObjectBMustImplement.IsSubsetOf(allInterfaces);
    }

}


//Creates an object of type ObjectA
var a = new ObjectA(); 

// Add requirement of IA
a.AddrequiredInterface(typeof(IA));

// Add requirement of IB
a.AddrequiredInterface(typeof(IB));

// This is true because a require IA and IB and ObjectB implements both
var canBeAddedInto = a.CanBeComsposedInto(new ObjectB());

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