将不同的函数称为getter

时间:2017-07-31 16:10:18

标签: c#

我有以下课程:

public class ContentVideoType
{
    public string Title { get; set; }
    public string GetThumbnail { get; set; }
}

创建此类的实例时,我想为GetThumbnail分配一个自定义的getter。我不知道它是如何调用的,但据我所知,代码必须如下:

var youtube = new ContentVideoType
{
    Title = "Youtube",
    GetThumbnail = (x) => { return $"https://i.ytimg.com/vi/{x}/mqdefault.jpg"; }
};

var vimeo = new ContentVideoType
{
    Title = "Vimeo",
    GetThumbnail = (x) => GetVimeoImage(x)
};

对于Viemo,我需要为GetThumbnail调用以下函数:

public static string GetVimeoImage(string vimeoId)
{
    string result = String.Empty;
    try
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("http://vimeo.com/api/v2/video/" + vimeoId + ".xml");

        XmlElement root = doc.DocumentElement;
        result = root.FirstChild.SelectSingleNode("thumbnail_medium").ChildNodes[0].Value;
    }
    catch
    {
        //cat with cheese on it's face fail
    }

    return result;
}

4 个答案:

答案 0 :(得分:2)

听起来你希望GetThumbnail是一个返回字符串的方法,而不仅仅是一个字符串。您可以通过这种方式重新声明:

public Func<string, string> GetThumbnail { get; set; }

这会导致此代码编译:

var youtube = new ContentVideoType
{
    Title = "Youtube",
    GetThumbnail = (x) => { 
        return string.Format("https://i.ytimg.com/vi/{0}/mqdefault.jpg", x); }
};

请注意,上面的GetThumbnail只接受一个接受一个参数并返回字符串的方法。

编辑:以下是如何使用它的示例:

string thumbnail = youtube.GetThumbnail("abc");

答案 1 :(得分:2)

我想,你想以某种奇怪的方式实现继承:)你应该在这里使用正确的继承(如果接收字符串作为参数,GetThumbnail应该是方法):

public abstract class ContentVideoType
{
    public virtual string Title { get; set; }
    public virtual string GetThumbnail(string id)
    {
        return "Some Default Thumbnail";
    }
}

public class YouTubeContentVideType : ContentVideoType
{
    public override string GetThumbnail(string id)
    {
        return "";//your logic for youTube
    }
}

public class VimeoContentVideType : ContentVideoType
{
    public override string GetThumbnail(string id)
    {
        return "";//your logic for vimeo
    }
}

=== UPDATE ===

根据您的最新回复 - 以下是它的外观:

        void Main()
        {           
            foreach (var videoType in GetAll)
            {
                Console.WriteLine(videoType.Title + " " + videoType.GetThumbnail("1")));
            }
        }

        public abstract class ContentVideoType
        {
            public virtual string Title { get; }
            public virtual string GetThumbnail(string id)
            {
                return "Some Default Thumbnail";
            }
        }

        public class YouTubeContentVideoType : ContentVideoType
        {
            public override string Title { get; } = "Youtube"; 

            public override string GetThumbnail(string id)
            {
                return $"https://i.ytimg.com/vi/{id}/mqdefault.jpg";
            }
        }

        public class VimeoContentVideType : ContentVideoType
        {
            public override string Title { get; } = "Vimeo";

            public override string GetThumbnail(string id)
            {
                return GetVimeoPreview(id);
            }

            public string GetVimeoPreview(string videoId)
            {
                return $"url:{videoId}"; //your code here;
            }
        }

        public static List<ContentVideoType> GetAll
        {
            get
            {
                var result = new List<ContentVideoType>
                {
                    new YouTubeContentVideoType(),
                    new VimeoContentVideType()
                };

                return result;
            }
        }

答案 2 :(得分:0)

您可以为ContentVideoType创建两个子类,每个子类实现自己的GetThumbnail版本吗?

否则,能够用反射交换getter的事实似乎是不可能的:https://stackoverflow.com/a/6835824/2525304

答案 3 :(得分:0)

好的,这是我如何通过一些帮助和猜测来做到这一点:)

public class ContentVideoType
{
    public string Title { get; set; }

    public Func<string, string> GetThumbnail { get; set; }
}

public static List<ContentVideoType> GetAll
{
    get
    {
        var result = new List<ContentVideoType> {
            new ContentVideoType
            {
                Title = "Youtube",
                GetThumbnail = videoId => $"https://i.ytimg.com/vi/{videoId}/mqdefault.jpg"
            },

            new ContentVideoType
            {
                Title = "Vimeo",
                GetThumbnail = videoId => GetVimeoPreview(videoId)
            }
        };

        return result;
    }
}