私はインターフェースを持っています:
public interface IBag
{
public string BagName { get; }
}
クラスはそれから継承します:
public class CacheBag : IBag
{
private Dictionary<string, object> cache;
public Dictionary<string, object> Cache
{
get
{
return this.cache;
}
private set
{
this.cache = value;
}
}
public string BagName
{
get { return "CacheBag"; }
}
}
インターフェイスから継承するクラスの拡張メソッドを作成しようとしています:
public static object Retrieve(this IBag bag)
{
Type objType = bag.GetType();
IBag obj = null;
try
{
IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
string fileName = string.Format(CultureInfo.InvariantCulture, "{0}.xml", bag.BagName);
if (appStore.FileExists(fileName))
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, appStore))
{
using (StreamReader sr = new StreamReader(isoStream))
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objType);
obj = (IBag)x.Deserialize(sr);
}
}
}
}
catch (Exception ex)
{
log.Error(ex.Message);
}
return obj;
}
}
しかし、今では次のように機能しました。
UserBag users = new UserBag();
users.Retrieve();
次のような拡張機能を呼び出すことができます。
CacheBag.Retrieve();
それを達成するために実装をどのように変更すればよいですか?