Test | |
---|---|
Field1 | 12 |
Field2 | 3456789 |
Field3 | 0ABCD |
Field4 | EFGHIJKLMN |
Field5 | O |
Field6 | P |
Field7 | QRSTUVWX |
Field8 | YZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ |
Field9 | |
Field10 |
Created
July 21, 2021 23:52
-
-
Save sean-gilliam/b9f1ce986aaf328fb077223ea5fb1319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
using System.Runtime.Serialization; | |
void Main() | |
{ | |
var input = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var test = Adapter<Test>(input); | |
Console.WriteLine(test); | |
} | |
public T Adapter<T>(string input) where T : class, new() | |
{ | |
var obj = new T(); | |
var type = obj.GetType(); | |
var properties = type | |
.GetProperties() | |
.Select((x, i) => new { | |
Name = x.Name, | |
Length = x.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().SingleOrDefault()?.MaximumLength ?? 1, | |
Order = x.GetCustomAttributes(typeof(DataMemberAttribute), false).Cast<DataMemberAttribute>().SingleOrDefault()?.Order ?? i | |
}).OrderBy(x => x.Order); | |
var length = properties.Sum(x => x.Length); | |
if(input.Length < length) | |
input = input.PadRight(length, ' '); | |
var idx = 0; | |
foreach(var property in properties) | |
{ | |
var prop = type.GetProperty(property.Name); | |
prop.SetValue(obj, input.Substring(idx, property.Length), null); | |
idx += property.Length; | |
} | |
return obj; | |
} | |
[DataContract] | |
public class Test | |
{ | |
[DataMember(Order = 0, Name = "TestFieldOne", IsRequired = true), StringLength(2)] | |
public string Field1 { get; set; } | |
[DataMember(Order = 1, Name = "TestFieldTwo", IsRequired = true), StringLength(7)] | |
public string Field2 { get; set; } | |
[DataMember(Order = 2, Name = "TestFieldThree", IsRequired = true), StringLength(5)] | |
public string Field3 { get; set; } | |
[DataMember(Order = 3, Name = "TestFieldFour", IsRequired = true), StringLength(10)] | |
public string Field4 { get; set; } | |
[DataMember(Order = 4, Name = "TestFieldFive", IsRequired = true), StringLength(1)] | |
public string Field5 { get; set; } | |
[DataMember(Order = 5, Name = "TestFieldSix", IsRequired = true), StringLength(1)] | |
public string Field6 { get; set; } | |
[DataMember(Order = 6, Name = "TestFieldSeven", IsRequired = true), StringLength(8)] | |
public string Field7 { get; set; } | |
[DataMember(Order = 7, Name = "TestFieldEight", IsRequired = true), StringLength(270)] | |
public string Field8 { get; set; } | |
[DataMember(Order = 8, Name = "TestFieldNine", IsRequired = true), StringLength(12)] | |
public string Field9 { get; set; } | |
[DataMember(Order = 9, Name = "TestFieldTen", IsRequired = true), StringLength(27)] | |
public string Field10 { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment