如何将基于类型的列表动态转换为派生类型

时间:2016-07-05 08:18:48

标签: c# .net oop interface polymorphism

我有一个带有以下层次结构的dll:

Interface ISchema {}

class Schema:ISchema{}

class TableSchema:Schema{}

class ViewSchema:Schema{}

我有另一个dll具有以下层次结构:

Interface ISearch {}

class Table:ISearch{}

class View:ISearch{}

以下代码根据用户选择触发Table或View上的搜索操作:

private void FindNowButton_Click(object sender, EventArgs e)
{
    // return Table or View according to user selection. (Property is an internal class helping to retrieve the selected type)
    var type = (ObjectsTypeComboBox.SelectedItem as Property).Type;

    // Create an instance of table or View as ISearch
    var instance = (ISearch)Activator.CreateInstance(type);

    // Call to relevant Search (table.Search or View.Search)
    // _dataManager help to get the records from Schema hierarchy
    // text is the text to search
    var result = instance.Search(_dataManager, FindWhatTextBox.Text);

    // Show in DataGridView the result
    FindResultsDGV.DataSource = result;
}

每个搜索方法都返回一个List。我需要在网格上显示不同的列。 TableSchema和ViewSchema具有不同的属性,如下所示进行转换。

FindResultsDGV.DataSource = result.Cast<TableSchema> ; // or result.Cast<ViewSchema>

如何在此阶段动态获取正确的类型?

欢迎任何其他解决方案

更新

根据@GiladGreen的说法

public interface ISearchSchemaFactory
{
    ISearch<ISchema> GetSearch(Type schemaType);
}

public class Factory : ISearchSchemaFactory
{
    public ISearch<ISchema> GetSearch(Type schemaType)
    {
        if (schemaType.Equals(typeof(Table)))
        {
            return new BL.AdvancedSearch.Table(); // Getting an error here
           // Cannot implicitly convert type 'Table' to 'ISearch<ISchema>'. An explicit conversion exists (are you missing a cast?) 
        }
        else if (schemaType.Equals(typeof(View)))
        {
            // TODO
        }

        return null; // TODO
    }
}

1 个答案:

答案 0 :(得分:1)

我建议您将ISearch更改为:

Interface ISearch<out TSchema> where TSchema: ISchema
{
    TSchema Search(....);   
}

class Table : ISearch<TableSchema>
{
    public TableSchema Search(....)
    {
        //Some searching code
    }
}

class View:ISearch<ViewSchema>
{
    public ViewSchema Search(....)
    {
        //Some searching code
    }
}

然后您还可以使用ISearchSchemaFactory根据您提供的ISearch为您提供所需的正确TSchema的实例:

public interface ISearchSchemaFactory
{
    ISearch<ISchema> GetSearch(Type schemaType);
}

用法:var search = factory.GetSearch(type);

Factoy实施示例

public class MappingSearchSchemaFactory : ISearchSchemaFactory
{
    public MappingSearchSchemaFactory(Dictionary<Type, ISearch<ISchema>> mapping)
    {
        Mapping = mapping;
    }

    ISearch<ISchema> GetSearch(Type schemaType)
    {
        ISearch<ISchema> result;
        if(!Mapping.TryGetValue(schemaType, out result)
        {
            //Some logging or throwing exception behavior - depends what you want
        }
        return result;
    }
    public Dictionary<Type, ISearch<ISchema>> Mapping { get; set; }
}

此具体实施适用于某人&#34;映射。可能的初始化代码可以是:

ISearchSchemaFactory factory = new MappingSearchSchemaFactory(
    new Dictionary<Type,ISearch<ISchema>>
    { new TableSchema(), new Table() },
    { new ViewSchema(), new view() }
);

但我不太推荐这个。我要查看Dependency InjectionIoC Containers来管理对象的初始化。我个人使用Castle Windsor