You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在同一类中多次导出相同元数据?能否重复导出元数据视图至单个对象?

Can You Merge Multiple Exports of the Same Metadata View into a Single Metadata Object?

Absolutely! This is totally feasible, and the approach depends on how you're handling metadata exports in your tech stack. Let’s walk through a common scenario (using .NET’s MEF as an example, since it’s widely used for metadata-driven architectures) and break down how to achieve your goal of splitting multiple string lists into logical properties in a merged metadata object.

Step 1: Define Your Metadata View and Export Attribute

First, create a base metadata view that represents a single string list, plus a way to identify which logical property it belongs to:

public interface IStringListMetadata
{
    IEnumerable<string> Items { get; }
    string TargetProperty { get; } // Maps this list to a specific property in the merged object
}

// Custom export attribute that implements the metadata view
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ExportStringListAttribute : ExportAttribute, IStringListMetadata
{
    public ExportStringListAttribute(string targetProperty, params string[] items)
        : base(typeof(IStringListMetadata))
    {
        TargetProperty = targetProperty;
        Items = items.AsEnumerable();
    }

    public IEnumerable<string> Items { get; }
    public string TargetProperty { get; }
}

Step 2: Export Multiple Instances of the Metadata View

Now, export your string lists using the custom attribute, specifying which logical property each list maps to:

[ExportStringList("CategoryTags", "Electronics", "HomeGoods", "Apparel")]
[ExportStringList("ProductIds", "P1001", "P1002", "P1003")]
[ExportStringList("RegionCodes", "NA", "EU", "APAC")]
public class MyDataSource
{
    // Your class implementation goes here
}

Step 3: Import and Merge the Metadata

When importing, collect all exported metadata instances and merge them into a single object with distinct, logical properties:

public class MetadataAggregator
{
    [ImportMany]
    public IEnumerable<Lazy<object, IStringListMetadata>> StringListExports { get; set; }

    public MergedMetadata GetMergedMetadata()
    {
        var merged = new MergedMetadata();
        
        foreach (var export in StringListExports)
        {
            var metadata = export.Metadata;
            switch (metadata.TargetProperty)
            {
                case "CategoryTags":
                    merged.CategoryTags = metadata.Items.ToList();
                    break;
                case "ProductIds":
                    merged.ProductIds = metadata.Items.ToList();
                    break;
                case "RegionCodes":
                    merged.RegionCodes = metadata.Items.ToList();
                    break;
                // Add cases for additional properties as needed
            }
        }
        
        return merged;
    }
}

// The final merged metadata object with logical, split properties
public class MergedMetadata
{
    public List<string> CategoryTags { get; set; } = new();
    public List<string> ProductIds { get; set; } = new();
    public List<string> RegionCodes { get; set; } = new();
}

Adaptations for Other Stacks

If you’re using a different technology, the core idea stays the same:

  • TypeScript/JavaScript (Decorators): Create a decorator that accepts a target property name and string list, collect all decorated metadata entries at runtime, then merge them into a single object.
  • Custom Metadata Systems: Include a unique identifier (like TargetProperty in the example) with each export, then aggregate exports by that identifier into your final structure.
  • EF Core Entity Metadata: Use ModelBuilder to add multiple metadata entries, then query and map them to a merged object during model initialization.

The key is adding a way to associate each metadata export with the logical property it should map to in your final merged object—this lets you cleanly split multiple string lists into meaningful properties while reusing the same base metadata view.

内容的提问来源于stack exchange,提问作者William Thomas Waller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:14:41