Skip to content

Instantly share code, notes, and snippets.

@scionwest
Created January 17, 2015 19:58
Show Gist options
  • Save scionwest/483fba984222c7662d17 to your computer and use it in GitHub Desktop.
Save scionwest/483fba984222c7662d17 to your computer and use it in GitHub Desktop.
public interface IMessage
{
T GetContent<T>() where T : class;
}
public abstract class MessageBase<TContentType> : IMessage where TContentType : class
{
TContentType Content { get; }
public abstract TContentType GetContent<ContentType>();
// I want to replace this with the GetContent above
public T GetContent<T>() where T : class
{
throw new NotImplementedException();
}
}
@jakesays-old
Copy link

public interface IMessage
{
    object GetContent();
}

public interface IMessage<T> : IMessage where T : class
{
    T GetContent();
}

public abstract class MessageBase<TContentType> : IMessage<TContentType> where TContentType : class
{
    TContentType Content { get; set; }

    public abstract TContentType GetContent();

    object IMessage.GetContent()
    {
        return GetContent();
    }
}

@scionwest
Copy link
Author

Another potential solution.

public interface IMessage
{
    object GetContent();
}

public interface ITypedMessage<TContentType> : IMessage where TContentType : class
{
    TContentType Content { get; }

    new TContentType GetContent();
}

public class WhisperMessage : ITypedMessage<string>
{
    public string Content
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public new string GetContent()
    {
        throw new NotImplementedException();
    }

    object IMessage.GetContent()
    {
        throw new NotImplementedException();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment