简体   繁体   English

使用params关键字和动态类型参数

[英]Using params keyword with dynamic type arguments

I've declared an interface with Add method to add new item, 2 classes TopicModel and CommentModel to store data. 我已经使用Add方法声明了一个接口来添加新项目,2个类TopicModelCommentModel来存储数据。

I've re-write the code in console mode, like this: 我在控制台模式下重写代码,如下所示:

interface IAction
{
    void Add<T>(params T[] t) where T : class;
}

class TopicModel
{
    public string Id { get; set; }
    public string Author { get; set; }
}

class CommentModel
{
    public string Id { get; set; }
    public string Content { get; set; }
}

class Topic : IDisposable, IAction
{
    public void Add<T>(params T[] t) where T : class
    {
        var topic = t[0] as TopicModel;
        var comment = t[1] as CommentModel;

        // do stuff...
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

class MainClass
{
    static void Main()
    {
        var t = new TopicModel { Id = "T101", Author = "Harry" };
        var c = new CommentModel { Id = "C101", Content = "Comment 01" };

        using (var topic = new Topic())
        {
            //topic.Add(t, c);
        }
    }
}

The line topic.Add(t, c) gave me error message: topic.Add(t, c)给了我错误信息:

The type arguments for method 'Topic.Add(params T[])' cannot be inferred from the usage. 无法从用法推断出方法'Topic.Add(params T [])'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

Then, I've tried again with: 然后,我再次尝试:

topic.Add(c, c) // Good :))
topic.Add(t, t, t); // That's okay =))

That's my problem. 那是我的问题。 I want the method accepts 2 different types ( TopicModel and CommentModel ). 我希望该方法接受2种不同的类型( TopicModelCommentModel )。

And, I don't want to declare: 并且,我不想声明:

interface IAction
{
    void Add(TopicModel t, CommentModel c);
}

because another classes can re-use Add method within different argument types. 因为另一个类可以在不同的参数类型中重用Add方法。

So, my question is: how to change params T[] t to accept multiple argument types? 所以,我的问题是:如何更改params T[] t以接受多个参数类型?

TopicModel et CommentModel must inherit same class Or implement same interface. TopicModel和CommentModel必须继承相同的类或实现相同的接口。 Try This : 尝试这个 :

interface IAction
{
    void Add<T>(params T[] t) where T : IModel;
}

class IModel
{
}

class TopicModel : IModel
{
    public string Id { get; set; }
    public string Author { get; set; }
}

class CommentModel : IModel
{
    public string Id { get; set; }
    public string Content { get; set; }
}

class Topic : IDisposable, IAction
{
    public void Add<T>(params T[] t) where T : IModel
    {
        var topic = t[0] as TopicModel;
        var comment = t[1] as CommentModel;

        Console.WriteLine("Topic witch ID={0} added",topic.Id);
        Console.WriteLine("Commment witch ID={0} added", comment.Id);
    }

    public void Dispose()
    {

    }
}

class Program
{
    static void Main()
    {
        TopicModel t = new TopicModel { Id = "T101", Author = "Harry" };
        CommentModel c = new CommentModel { Id = "C101", Content = "Comment 01" };

        using (var topic = new Topic())
        {
            topic.Add<IModel>(t, c);
        }

        Console.ReadLine();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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