可以简化此编码 [英] Possible to simplify this coding
本文介绍了可以简化此编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的目标是将类成员名称转换为蛇形字符串
My aim is convert class member names to snake case strings
为此,我使用了反射和一些字符串转换器功能.它工作正常,但我认为它可以以某种简化的方式存在.
For this I use reflection and some string converter function. It's working properly but I think it could be exist some simplified way.
我分享了我的功能及其用法.问题=>使用时间很长,并且< User>定义.
I share my functions and their usage. The problem => usage is very long and there is duplicate <User> defining.
CustomConvert.GetPsqlColumnName<User>(StaticReflection.GetMemberName<User>(p => p.UserName))
输入为:"UserName";
Input is : "UserName"
输出为:用户名""
public class CustomConvert
{
public static string GetPsqlColumnName<T>(string memberName)
{
if (memberName == "Id")
{
var tableName = typeof(T).Name;
return $"{PsqlNaming(tableName)}_id";
}
else
{
return PsqlNaming(memberName);
}
}
private static string PsqlNaming(string name)
{
var sb = new StringBuilder();
foreach (var item in name.ToCharArray())
{
var val = item.ToString();
if (char.IsUpper(item))
{
if (string.IsNullOrEmpty(sb.ToString())) val = item.ToString().ToLower();
else val = "_" + item.ToString().ToLower();
}
sb.Append(val);
}
return sb.ToString();
}
}
public static class StaticReflection
{
public static string GetMemberName<T>(
this T instance,
Expression<Func<T, object>> expression)
{
return GetMemberName(expression);
}
public static string GetMemberName<T>(
Expression<Func<T, object>> expression)
{
if (expression == null)
{
throw new ArgumentException(
"The expression cannot be null.");
}
return GetMemberName(expression.Body);
}
public static string GetMemberName<T>(
this T instance,
Expression<Action<T>> expression)
{
return GetMemberName(expression);
}
public static string GetMemberName<T>(
Expression<Action<T>> expression)
{
if (expression == null)
{
throw new ArgumentException(
"The expression cannot be null.");
}
return GetMemberName(expression.Body);
}
private static string GetMemberName(
Expression expression)
{
if (expression == null)
{
throw new ArgumentException(
"The expression cannot be null.");
}
if (expression is MemberExpression)
{
// Reference type property or field
var memberExpression =
(MemberExpression)expression;
return memberExpression.Member.Name;
}
if (expression is MethodCallExpression)
{
// Reference type method
var methodCallExpression =
(MethodCallExpression)expression;
return methodCallExpression.Method.Name;
}
if (expression is UnaryExpression)
{
// Property, field of method returning value type
var unaryExpression = (UnaryExpression)expression;
return GetMemberName(unaryExpression);
}
throw new ArgumentException("Invalid expression");
}
private static string GetMemberName(
UnaryExpression unaryExpression)
{
if (unaryExpression.Operand is MethodCallExpression)
{
var methodExpression =
(MethodCallExpression)unaryExpression.Operand;
return methodExpression.Method.Name;
}
return ((MemberExpression)unaryExpression.Operand)
.Member.Name;
}
}
推荐答案
代码似乎还可以,可以通过简化代码来详细说明您的意思吗?
The code seems to be OK as is, can you elaborate more what you mean by simplifying it?
这篇关于可以简化此编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文