您可以尝试使用反射:
string[] notRequiredExportProperties = new string[]
{
"Record",
"ContentItem",
"Zones",
"Id",
"TypeDefinition",
"TypePartDefinition",
"PartDefinition",
"Settings",
"Fields"
};
protected override void Importing(ContactPart part, Orchard.ContentManagement.Handlers.ImportContentContext context)
{
var contactRecordType = part.Record.GetType();
var allProps = contactRecordType.GetProperties();
foreach (PropertyInfo p in allProps)
{
if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1)
continue;
var importValue = context.Attribute(part.PartDefinition.Name, p.Name);
var import = Convert.ChangeType(importValue, p.PropertyType);
if (p.PropertyType.IsSubclassOf(typeof(Enum)))
continue;
p.SetValue(part.Record, import, null);
}
}
protected override void Exporting(ContactPart part, Orchard.ContentManagement.Handlers.ExportContentContext context)
{
var contactPartType = part.GetType();
var allProps = contactPartType.GetProperties();
foreach(PropertyInfo p in allProps)
{
if (Array.FindIndex(notRequiredExportProperties, i => i == p.Name) > -1)
continue;
var propVal = p.GetValue(part, null);
context.Element(part.PartDefinition.Name).SetAttributeValue(p.Name, propVal);
}
}
您可能需要做一些额外的工作才能使其支持 Enums 等。这可能应该在某种辅助类中。