I've been struggling with a piece of C# code and although I have found a solution to the problem, it is by no means ideal (see DoSomething_WorksButNotIdeal() below).
What I would like to do is instead of having the if, else statement (which is potentially massive depending on what types I want to support) just have a generic cast, but I can't get it to work. I've tried to demonstrate this in the DoSomething_HelpMe() method.
Is there anyway of achieving this? Any help is greatly appreciated.
public interface ITag
{
string TagName { get; }
Type Type { get; }
}
public interface ITag<T> : ITag
{
T InMemValue { get; set; }
T OnDiscValue { get; set; }
}
public class Tag<T> : ITag<T>
{
public Tag(string tagName)
{
TagName = tagName;
}
public string TagName { get; private set; }
public T InMemValue { get; set; }
public T OnDiscValue { get; set; }
public Type Type{ get{ return typeof(T);} }
}
public class MusicTrack
{
public MusicTrack()
{
TrackTitle = new Tag<string>("TrackTitle");
TrackNumber = new Tag<int>("TrackNumber");
Tags = new Dictionary<string, ITag>();
Tags.Add(TrackTitle.TagName, TrackTitle);
Tags.Add(TrackNumber.TagName, TrackNumber);
}
public IDictionary<string,ITag> Tags;
public ITag<string> TrackTitle { get; set; }
public ITag<int> TrackNumber { get; set; }
}
public static class Main
{
public static void DoSomething_WorksButNotIdeal()
{
MusicTrack track1 = new MusicTrack();
MusicTrack track2 = new MusicTrack();
// Set some values on the tracks
foreach (ITag tag in track1.Tags.Values)
{
Type type = tag.Type;
if (type == typeof(string))
{
((ITag<string>) tag).InMemValue = ((ITag<string>)track2.Tags[tag.TagName]).OnDiscValue;
}
else if (type == typeof(int))
{
((ITag<int>)tag).InMemValue = ((ITag<int>)track2.Tags[tag.TagName]).OnDiscValue;
}
else if (type == typeof(bool))
{
((ITag<bool>)tag).InMemValue = ((ITag<bool>)track2.Tags[tag.TagName]).OnDiscValue;
}
// etc etc
else
{
throw new Exception("Unsupported type.");
}
}
}
public static void DoSomething_HelpMe()
{
MusicTrack track1 = new MusicTrack();
MusicTrack track2 = new MusicTrack();
// Set some values on the tracks
foreach (ITag tag in track1.Tags.Values)
{
Type type = tag.Type;
// THIS OBVIOUSLY DOESN'T WORK BUT I'M JUST TRYING TO DEMONSTRATE WHAT
// I'D IDEALLY LIKE TO ACHIEVE
((ITag<typeof(type)>)tag).InMemValue = ((ITag<typeof(type)>)track2.Tags[tag.TagName]).OnDiscValue;
}
}
}
Any reason that you can't have:
public interface ITag
{
string TagName { get; }
Type Type { get; }
object InMemValue { get; set; }
object OnDiscValue { get; set; }
}
and use ITag<T>
to make it more specific?
public interface ITag<T> : ITag
{
new T InMemValue { get; set; }
new T OnDiscValue { get; set; }
}
Then your method can just use ITag
. You'd need something like (int Tag<T>
):
object ITag.InMemValue
{
get { return InMemValue; }
set { InMemValue = (T)value; }
}
object ITag.OnDiscValue
{
get { return OnDiscValue; }
set { OnDiscValue = (T)value; }
}
(edit)
Another option would be a method on the non-generic ITag
:
void CopyValueFrom(ITag tag);
(maybe a bit more specific about what it copies to/from)
Your concrete implementation ( Tag<T>
) would have to assume that the ITag
is actually an ITag<T>
and cast:
public void CopyFromTag(ITag tag) {
ITag<T> from = tag as ITag<T>;
if(from==null) throw new ArgumentException("tag");
this.TheFirstProperty = from.TheSecondProperty;
}
The simplest way to solve it is to resolve the type where you have the information, namely inside the Tag<T>
implementation, so add the following to your existing types (only showing the additions!)
public interface ITag
{
void CopyFrom(bool sourceIsMem, ITag sourceTag, bool targetIsMem);
}
public class Tag<T> : ITag<T>
{
public void CopyFrom(bool sourceIsMem, ITag sourceTag, bool targetIsMem)
{
ITag<T> castSource = sourceTag as ITag<T>;
if (castSource == null)
throw new ArgumentException(
"Source tag is of an incompatible type", "sourceTag");
if (targetIsMem)
InMemValue = sourceIsMem ?
castSource.InMemValue : castSource.OnDiscValue;
else
OnDiscValue = sourceIsMem ?
castSource.InMemValue : castSource.OnDiscValue;
}
}
Note that you really should use enum
types for the sourceIsMem
and targetIsMem
instead, because a bool
is really ugly and hard to read in the invocation as the following fragment will show.
This is how you would make your routine work now:
public static void DoSomething_HelpMe()
{
MusicTrack track1 = new MusicTrack();
MusicTrack track2 = new MusicTrack();
// Set some values on the tracks
foreach (ITag tag in track1.Tags.Values)
tag.CopyFrom(false, track2.Tags[tag.TagName], true);
}
Here's one approach, which requires a decent amount of boilerplate but will allow you to do what you want using your existing definitions of ITag
, ITag<T>
, and Tag<T>
. The TagSetter
class sets the in memory value from the on disc value in a type safe way for any ITag<T>
.
/// <summary>
/// Allows a tag of any type to be used to get a result of type TResult
/// </summary>
/// <typeparam name="TResult">The result type after using the tag</typeparam>
public interface ITagUser<TResult>
{
TResult Use<T>(ITag<T> tag);
}
/// <summary>
/// Allows a tag of any type to be used (with no return value)
/// </summary>
public interface ITagUser
{
void Use<T>(ITag<T> tag);
}
/// <summary>
/// Wraps a tag of some unknown type. Allows tag users (either with or without return values) to use the wrapped list.
/// </summary>
public interface IExistsTag
{
TResult Apply<TResult>(ITagUser<TResult> user);
void Apply(ITagUser user);
}
/// <summary>
/// Wraps a tag of type T, hiding the type itself.
/// </summary>
/// <typeparam name="T">The type of element contained in the tag</typeparam>
class ExistsTag<T> : IExistsTag
{
ITag<T> tag;
public ExistsTag(ITag<T> tag)
{
this.tag = tag;
}
#region IExistsTag Members
public TResult Apply<TResult>(ITagUser<TResult> user)
{
return user.Use(tag);
}
public void Apply(ITagUser user)
{
user.Use(tag);
}
#endregion
}
public interface ITag
{
string TagName { get; }
Type Type { get; }
}
public interface ITag<T> : ITag
{
T InMemValue { get; set; }
T OnDiscValue { get; set; }
}
public class Tag<T> : ITag<T>
{
public Tag(string tagName)
{
TagName = tagName;
}
public string TagName { get; private set; }
public T InMemValue { get; set; }
public T OnDiscValue { get; set; }
public Type Type { get { return typeof(T); } }
}
public class TagSetter : ITagUser
{
#region ITagUser Members
public void Use<T>(ITag<T> tag)
{
tag.InMemValue = tag.OnDiscValue;
}
#endregion
}
public class TagExtractor : ITagUser<ITag>
{
#region ITagUser<ITag> Members
public ITag Use<T>(ITag<T> tag)
{
return tag;
}
#endregion
}
public class MusicTrack
{
public MusicTrack()
{
TrackTitle = new Tag<string>("TrackTitle");
TrackNumber = new Tag<int>("TrackNumber");
Tags = new Dictionary<string, IExistsTag>();
Tags.Add(TrackTitle.TagName, new ExistsTag<string>(TrackTitle));
Tags.Add(TrackNumber.TagName, new ExistsTag<int>(TrackNumber));
}
public IDictionary<string, IExistsTag> Tags;
public ITag<string> TrackTitle { get; set; }
public ITag<int> TrackNumber { get; set; }
}
public static class Main
{
public static void DoSomething_WorksButNotIdeal()
{
MusicTrack track1 = new MusicTrack();
MusicTrack track2 = new MusicTrack();
TagSetter setter = new TagSetter();
TagExtractor extractor = new TagExtractor();
// Set some values on the tracks
foreach (IExistsTag tag in track1.Tags.Values)
{
tag.Apply(setter);
// do stuff using base interface if necessary
ITag itag = tag.Apply(extractor);
}
}
}
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.