基本上,我想在班上做这样的事情(我知道这不起作用,因为它不是一个常数)。。。。
[XmlStorage(IsSingleStorageFile = false, IsSubordinate = true,
StorageLocation = "posts" + GetBlogId()]
其中GetBlogId()将是一个静态实用程序方法。
我正在为我正在编写的博客引擎构建一个xml存储框架(一部分是学习练习,一部分是因为我想把一些东西还给开源),我认为确定存储位置的最整洁的方法是使用自定义属性,因为我无论如何都要使用datacontractserializer。
我的存储路径是这样的。。。
文章\博客ID\postid.xml文件
通过解析url并返回关联的blog来确定blog id的位置。这将允许我在一个安装中托管多个博客,同时保持post存储文件的独立性,以减少加载post时的内存开销。
这是直接拒绝还是有更好的方法让我去做我正在尝试的事情?
编辑:
根据约翰的回答,我试过这个。。。。
private static string GetSubordinatePath(Type type)
{
if (typeof(ISubordinate).IsAssignableFrom(type))
{
object instance = Activator.CreateInstance(type);
return (instance as ISubordinate).ParentGuid.ToString();
}
else
{
// TODO: Localize this.
throw new ArgumentException(
String.Format(
CultureInfo.CurrentCulture,
"The specified type '{0}' does not impliment the ISubordinate interface. Please edit the source appropriately to enable storage.",
type.GetType().Name));
}
}
这很管用。。