-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Конина Анастасия #197
base: master
Are you sure you want to change the base?
Конина Анастасия #197
Changes from all commits
8f23280
3c24b29
55e2b83
c9c9db1
0948724
71286e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ObjectPrinting | ||
{ | ||
public interface IPropertyPrintingConfig<TOwner, TPropType> | ||
{ | ||
PrintingConfig<TOwner> ParentConfig { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ObjectPrinting | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
public static string PrintToString<T>(this T obj) | ||
{ | ||
return ObjectPrinter.For<T>().PrintToString(obj); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,99 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PrintingConfig<TOwner> | ||
{ | ||
private readonly HashSet<Type> excludedTypes = new HashSet<Type>(); | ||
private readonly HashSet<MemberInfo> excludedMembers = new HashSet<MemberInfo>(); | ||
|
||
private readonly Dictionary<Type, Func<object, string>> typePrintingRules = | ||
new Dictionary<Type, Func<object, string>>(); | ||
|
||
private readonly Dictionary<MemberInfo, Func<object, string>> memberPrintingRules = | ||
new Dictionary<MemberInfo, Func<object, string>>(); | ||
|
||
private readonly Dictionary<Type, CultureInfo> typeCultures = new Dictionary<Type, CultureInfo>(); | ||
private readonly Dictionary<MemberInfo, CultureInfo> memberCultures = new Dictionary<MemberInfo, CultureInfo>(); | ||
|
||
private readonly Dictionary<MemberInfo, Func<string, string>> memberTrimToLength = | ||
new Dictionary<MemberInfo, Func<string, string>>(); | ||
|
||
private readonly Dictionary<Type, Func<string, string>> typeTrimToLength = | ||
new Dictionary<Type, Func<string, string>>(); | ||
|
||
private MemberInfo currentMember; | ||
|
||
public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>() | ||
{ | ||
return new PropertyPrintingConfig<TOwner, TPropType>(this); | ||
} | ||
|
||
public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>( | ||
Expression<Func<TOwner, TPropType>> memberSelector) | ||
{ | ||
currentMember = ((MemberExpression) memberSelector.Body).Member; | ||
return new PropertyPrintingConfig<TOwner, TPropType>(this); | ||
} | ||
|
||
public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector) | ||
{ | ||
currentMember = ((MemberExpression) memberSelector.Body).Member; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишнее вообще канеш так делать не гуд, эту штука скорее должна вернуть какой то контекст, в котором будут настраивать параметры конкретного поля |
||
excludedMembers.Add(currentMember); | ||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> Excluding<TPropType>() | ||
{ | ||
excludedTypes.Add(typeof(TPropType)); | ||
return this; | ||
} | ||
|
||
public void SetPrintingRule<TPropType>(Func<TPropType, string> print) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот эта штука сейчас торчит наружу, не оч хорошо, синтаксис твоей либы позволяет написать вот так, и это ничего не дает
|
||
{ | ||
if (currentMember == null) | ||
typePrintingRules[typeof(TPropType)] = x => print((TPropType) x); | ||
else | ||
memberPrintingRules[currentMember] = x => print((TPropType) x); | ||
currentMember = null; | ||
} | ||
|
||
public void SetCulture<TPropType>(CultureInfo culture) | ||
{ | ||
if (currentMember == null) | ||
typeCultures[typeof(TPropType)] = culture; | ||
else | ||
memberCultures[currentMember] = culture; | ||
currentMember = null; | ||
} | ||
|
||
public void SetMaxLength<TPropType>(int maxLen) | ||
{ | ||
if (currentMember == null) | ||
typeTrimToLength[typeof(TPropType)] = x => x.Substring(0, Math.Min(x.Length, maxLen)); | ||
|
||
else | ||
memberTrimToLength[currentMember] = x => x.Substring(0, Math.Min(x.Length, maxLen)); | ||
|
||
|
||
currentMember = null; | ||
} | ||
|
||
public string PrintToString(TOwner obj) | ||
{ | ||
return PrintToString(obj, 0); | ||
} | ||
|
||
private string PrintToString(object obj, int nestingLevel) | ||
{ | ||
//TODO apply configurations | ||
if (obj == null) | ||
return "null" + Environment.NewLine; | ||
|
||
var finalTypes = new[] | ||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), | ||
typeof(DateTime), typeof(TimeSpan) | ||
}; | ||
if (finalTypes.Contains(obj.GetType())) | ||
return obj + Environment.NewLine; | ||
|
||
var identation = new string('\t', nestingLevel + 1); | ||
var sb = new StringBuilder(); | ||
var type = obj.GetType(); | ||
sb.AppendLine(type.Name); | ||
foreach (var propertyInfo in type.GetProperties()) | ||
{ | ||
sb.Append(identation + propertyInfo.Name + " = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
} | ||
return sb.ToString(); | ||
var serialization = new Serialization(new SerializationConfig(excludedTypes, | ||
excludedMembers, | ||
typePrintingRules, | ||
memberPrintingRules, | ||
typeCultures, | ||
memberCultures, | ||
memberTrimToLength, | ||
typeTrimToLength)); | ||
return serialization.Serialize(obj); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PropertyPrintingConfig<TOwner, TPropType> : IPropertyPrintingConfig<TOwner, TPropType> | ||
{ | ||
private readonly PrintingConfig<TOwner> printingConfig; | ||
|
||
public PropertyPrintingConfig(PrintingConfig<TOwner> printingConfig) | ||
{ | ||
this.printingConfig = printingConfig; | ||
} | ||
|
||
public PrintingConfig<TOwner> Using(Func<TPropType, string> print) | ||
{ | ||
printingConfig.SetPrintingRule(print); | ||
return printingConfig; | ||
} | ||
|
||
PrintingConfig<TOwner> IPropertyPrintingConfig<TOwner, TPropType>.ParentConfig => printingConfig; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public static class PropertyPrintingConfigExtensions | ||
{ | ||
public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) | ||
{ | ||
return config(ObjectPrinter.For<T>()).PrintToString(obj); | ||
} | ||
|
||
public static PrintingConfig<TOwner> SetMaxLength<TOwner, TPropType>( | ||
this PropertyPrintingConfig<TOwner, TPropType> propConfig, int maxLen) | ||
{ | ||
var propertyConfig = ((IPropertyPrintingConfig<TOwner, TPropType>) propConfig).ParentConfig; | ||
|
||
propertyConfig.SetMaxLength<TPropType>(maxLen); | ||
return propertyConfig; | ||
} | ||
|
||
public static PrintingConfig<TOwner> Using<TOwner, TPropType>( | ||
this PropertyPrintingConfig<TOwner, TPropType> propConfig, CultureInfo culture) | ||
where TPropType : IFormattable | ||
{ | ||
var printingConfig = ((IPropertyPrintingConfig<TOwner, TPropType>) propConfig).ParentConfig; | ||
printingConfig.SetCulture<TPropType>(culture); | ||
return printingConfig; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а что если пользователь сделает так?
итд