本文整理汇总了C#中IFormatter.Format方法的典型用法代码示例。如果您正苦于以下问题:C# IFormatter.Format方法的具体用法?C# IFormatter.Format怎么用?C# IFormatter.Format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFormatter
的用法示例。
在下文中一共展示了IFormatter.Format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Format
/// <inheritdoc />
public string Format(object obj, IFormatter formatter)
{
Type type = obj.GetType();
PropertyInfo keyProperty = type.GetProperty("Key");
PropertyInfo valueProperty = type.GetProperty("Value");
object key = keyProperty.GetValue(obj, null);
object value = valueProperty.GetValue(obj, null);
return string.Concat(formatter.Format(key), ": ", formatter.Format(value));
}
示例2: FormatMessage
public override string FormatMessage(IFormatter<object> formatter)
{
if(ActualValue == null)
return string.Format(ExpectMessages.EqualFormat, formatter.Format(null), formatter.Format(ExpectedString));
var n = ActualString.IndexOfDifference(ExpectedString);
var displayActual = formatter.Format(Center(ActualString, n, DisplayWidth));
var displayExpected = formatter.Format(Center(ExpectedString, n, DisplayWidth));
var format = string.Format("{0}\n{1}\n{2}^", Preamble, MessageFormat,
new string('-', displayActual.IndexOfDifference(displayExpected) + ExpectMessages.EqualFormat.IndexOf('{')));
return string.Format(format, displayActual, displayExpected);
}
示例3: Print
public void Print(Client client, IFormatter formatter)
{
var result = formatter.Format(client);
Console.WriteLine(result);
// class Print does nto suit format strings, so to keep OOP principles
// we create StringBasicFormat - a class only to format string when we need it
}
示例4: Format
/// <inheritdoc />
public string Format(object obj, IFormatter formatter)
{
var enumerable = (IEnumerable)obj;
var str = new StringBuilder();
str.Append('[');
bool first = true;
foreach (object value in enumerable)
{
if (first)
first = false;
else
str.Append(", ");
str.Append(formatter.Format(value));
}
str.Append(']');
return str.ToString();
}
示例5: FormatExpression
public virtual string FormatExpression(IFormatter<Expression> formatter)
{
return formatter.Format(body);
}
示例6: ToString
public string ToString(IFormatter<object> formatter)
{
return formatter.Format(Value);
}
示例7: FormatActual
public virtual string FormatActual(IFormatter<object> formatter)
{
return formatter.Format(Actual);
}
示例8: Format
static void Format(IFormatter f) => Console.WriteLine(f.Format(1, 2, 3));
示例9: FormatExpected
public override string FormatExpected(IFormatter<object> formatter)
{
return string.Format(methodDisplay(arguments), formatter.Format(arguments[0]));
}
示例10: Setup
public void Setup()
{
_baseDate = DateTime.Now;
_numberOfObjects = 10;
_objectsToFormat = new List<DummyObject>();
for (int i = 0; i < _numberOfObjects; i++)
{
_objectsToFormat.Add(new DummyObject()
{
SomeInt = i,
SomeString = i.ToString(),
SomeDateTime = _baseDate.AddDays(i),
CompositeObj = new DummyObject() {SomeInt = 10 + i}
});
}
_formatter = new Formatter<DummyObject>()
.With(y => y.SomeInt, 0, 5, y => y.ToString("00000"))
.With(y => y.SomeString, 2, 6, y => y.ToString().PadLeft(6, '_'))
.With(y => y.SomeDateTime, 1, 8, y => y.ToString(_dateFormat))
.WithDelimiter(";");
_result = _formatter.Format(_objectsToFormat);
}
示例11: FormatRecursive
private static string FormatRecursive(object obj, IFormatter formatter)
{
Type objType = obj.GetType();
var result = new StringBuilder();
result.Append('{');
result.Append(objType);
result.Append(": ");
var accessors = new List<KeyValuePair<string, Func<object, object>>>();
foreach (FieldInfo field in objType.GetFields(BindingFlags.Instance | BindingFlags.Public))
{
accessors.Add(new KeyValuePair<string, Func<object, object>>(field.Name, field.GetValue));
}
foreach (PropertyInfo property in objType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
PropertyInfo capturedProperty = property; // avoid unintended variable capture in the lambda.
if (property.CanRead && property.GetIndexParameters().Length == 0)
accessors.Add(new KeyValuePair<string, Func<object, object>>(property.Name,
x => capturedProperty.GetValue(x, null)));
}
accessors.Sort((a, b) => a.Key.CompareTo(b.Key));
for (int i = 0; i < accessors.Count; i++)
{
object value;
try
{
value = accessors[i].Value(obj);
}
catch (Exception)
{
continue;
}
if (i != 0)
result.Append(", ");
result.Append(accessors[i].Key);
result.Append(" = ");
result.Append(formatter.Format(value));
}
result.Append('}');
return result.ToString();
}
示例12: Format
/// <inheritdoc />
public string Format(object obj, IFormatter formatter)
{
var entry = (DictionaryEntry)obj;
return string.Concat(formatter.Format(entry.Key), ": ", formatter.Format(entry.Value));
}
示例13: FormatExpected
public override string FormatExpected(IFormatter<object> formatter) {
return string.Format("url ending with {0}", formatter.Format(arguments[1]));
}
示例14: FormatRecord
// This method takes an object that supports the IFormatter
// interface. It will call that object's Format() method.
private static void FormatRecord(IFormatter record)
{
record.Format();
}
示例15: Print
public void Print(string name, IFormatter formatter)
{
Console.WriteLine(formatter.Format(name));
}