简体   繁体   中英

Is there a way to extract a common interface from multiple classes in C#

I've got some code generated from an XSD, one of the properties is an array that can be one of 7 different classes, all these classes share some fields so I would like to create an interface so I can iterate over the array and access the 'tpl' property without having to do a huge switch statement and casting.

I've done this manually for now just for the 'tpl' property but I was wondering if there was a tool or way to paste all the classes into something and it would identify the common properties and generate an interface for me?

My Google fu is only comming up with things that will extract an interface from a single class or keep a class and interface in sync.

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("DT", typeof(DT2))]
        [System.Xml.Serialization.XmlElementAttribute("IP", typeof(IP2))]
        [System.Xml.Serialization.XmlElementAttribute("OPDT", typeof(OPDT2))]
        [System.Xml.Serialization.XmlElementAttribute("OPIP", typeof(OPIP2))]
        [System.Xml.Serialization.XmlElementAttribute("OPOR", typeof(OPOR2))]
        [System.Xml.Serialization.XmlElementAttribute("OR", typeof(OR2))]
        [System.Xml.Serialization.XmlElementAttribute("PP", typeof(PP2))]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }

Well I managed to write this myself, wasn't too hard after all.

Created a new Console App, added my other project as a reference. Add each Class to the list of lists then intersect each list with the previous to remove any items that don't exist in all Classes.

It's a bit rough and could be extended to public methods, and possibly a filter on the reflection to make sure the properties are actually public as well.

Would welcome any comments on how to improve it.

var listOfLists = new List<List<Properties>>();

listOfLists.Add(GetPropertyList<Class1>());
listOfLists.Add(GetPropertyList<Class2>());
listOfLists.Add(GetPropertyList<Class3>());


var output = Intersect(listOfLists);
WriteInterfaceOut(output);


static List<Properties>? Intersect(List<List<Properties>> listOfLists)
{
    List<Properties>? outputList = null;
    foreach (var item in listOfLists)
    {
        if (outputList == null)
        {
            outputList = item;
            continue;
        }

        outputList = outputList.Intersect(item, new Properties.Comparer()).ToList();
    }
    return outputList;
}

static void WriteInterfaceOut(List<Properties>? list)
{
    Console.WriteLine("public interface IRenameMe \r\n{");
    foreach (var item in list)
    {
        Console.WriteLine($"    public {item.TypeName} {item.Name} {{get; set;}}");
    }
    Console.WriteLine("}");
    Console.ReadKey();

}

static List<Properties> GetPropertyList<T>()
{
    var list = new List<Properties>();
    PropertyInfo[] myPropertyInfo;

    myPropertyInfo = typeof(T).GetProperties();
    for (int i = 0; i < myPropertyInfo.Length; i++)
    {
        list.Add(new Properties() { Name = myPropertyInfo[i].Name, TypeName = myPropertyInfo[i].PropertyType });
    }   
    return list;
}

public class Properties
{
    public string Name { get; set; }
    public Type TypeName { get; set; }


    public class Comparer : IEqualityComparer<Properties>
    {
        public bool Equals(Properties? x, Properties? y)
        {
            return x?.Name == y?.Name && x?.TypeName == y?.TypeName;
        }

        public int GetHashCode([DisallowNull] Properties obj)
        {
            unchecked
            {
                var hash = 17;
                hash = hash * 23 + obj.Name.GetHashCode();
                hash = hash * 23 + obj.TypeName.GetHashCode();
                return hash;
            }
        }
    }

}

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