mstaples84 / EdiHelper

EdiHelper is a C# Library to map objects of any type to EDIFACT Data and easily create documents in the EDI Format.
0 stars 0 forks source link

Multiple Edi tags on a property #1

Closed GH-Sensemaking closed 3 years ago

GH-Sensemaking commented 3 years ago

Hi Michel,

Thanks for your work on the project. I have a quick question that I am struggling with. Is it possible to have multiple edi tags on an object property? I notice that in your Test example you have multiple InvoiceNumber and InvoiceNumber2 which I imagine map to the same value.

Is it possible to do something like this

        [Edi(Tag = "UNB", Placeholder = "InvoiceNumber")]
        [Edi(Tag = "BGM", Placeholder = "InvoiceNumber")]
        public string InvoiceNumber { get; set; }
        public string InvoiceNumber2 { get; set; }

I am getting an exception when trying to run this tests: System.Reflection.AmbiguousMatchException: Multiple custom attributes of the same type found.

Which does make sense, but as far as I can tell the linq query should provide an enumerable of properties and attributes.

Thanks for any help you can give.

Michael

GH-Sensemaking commented 3 years ago

I solved my issue by making some edits to EdiObjectReader in the Read method.

I replaced the linq query with

var ediListGroup = o.GetType().GetProperties().Where(a => a.IsDefined(typeof(EdiAttribute), false)); var objList = ediListGroup.Select(p => p.GetCustomAttributes(typeof(EdiAttribute)).Select(at => new { Property = p, Attribute = (EdiAttribute)at }).ToList() ); var objListFlat = objList.SelectMany(x => x);

mstaples84 commented 3 years ago

Hello @GH-Sensemaking I'm happy you solved the issue. I hope this works good for you.