私はUmbraco4.7.1を使用しており、コンテンツノードをいくつかの自動生成された強い型のオブジェクトにマップしようとしています。valueinjecterとautomapperの両方を使用してみましたが、OOTBはどちらも私のプロパティをマップしません。Umbracoノード(cmsドキュメント)のすべてのプロパティが次のように取得されるためだと思います。
node.GetProperty("propertyName").Value;
そして、私の強く型付けされたオブジェクトは、MyObject.PropertyNameの形式です。では、メソッドと小文字で始まる文字列を使用して取得されたノードのプロパティを、プロパティが大文字で始まるMyObjectのプロパティにマップするにはどうすればよいですか?
更新 文字列プロパティを厳密に型指定されたプロパティにキャストする方法についてのインスピレーションを得るためにUmbracoソースコードを掘り下げて、意図したとおりにumbracoノードをマップする次のコードを作成することができました。
public class UmbracoInjection : SmartConventionInjection
{
protected override bool Match(SmartConventionInfo c)
{
return c.SourceProp.Name == c.TargetProp.Name;
}
protected override void Inject(object source, object target)
{
if (source != null && target != null)
{
Node node = source as Node;
var props = target.GetProps();
var properties = node.Properties;
for (int i = 0; i < props.Count; i++)
{
var targetProperty = props[i];
var sourceProperty = properties[targetProperty.Name];
if (sourceProperty != null && !string.IsNullOrWhiteSpace(sourceProperty.Value))
{
var value = sourceProperty.Value;
var type = targetProperty.PropertyType;
if (targetProperty.PropertyType.IsValueType && targetProperty.PropertyType.GetGenericArguments().Length > 0 && typeof(Nullable<>).IsAssignableFrom(targetProperty.PropertyType.GetGenericTypeDefinition()))
{
type = type.GetGenericArguments()[0];
}
targetProperty.SetValue(target, Convert.ChangeType(value, type));
}
}
}
}
}
ご覧のとおり、SmartConventionInjectionを使用して処理を高速化しています。16000個のオブジェクトのようなものをマップするのにまだ約20秒かかります。これをさらに速く行うことはできますか?
ありがとう
トーマス