有没有一种更好的方法来将虚线路径作为字符串?

时间:2018-07-16 15:10:06

标签: c#

是否有更好的方法将虚线路径转换为字符串?

public function store(Request $request,Post $post)
{
    $this->validate($request,[
        'website' => 'nullable|active_url',
        'comment' => 'required|min:5'
    ]);

    $comment = new Comment();
    $comment->user_id = Auth::user()->id;
    $comment->name = Auth::user()->fname;
    $comment->email = Auth::user()->email;
    $comment->website = $request->website;
    $comment->body = $request->comment;

    $post->comments()->save($comment);
    Session::flash('success', 'Comment Send Successfully');

    return back();
}

public function reply(Request $request,Comment $comment)
{
    $this->validate($request,[
        'website' => 'nullable|active_url',
        'replyComment' => 'required|min:5'
    ]);

    $reply = new Comment();
    $reply->user_id = Auth::user()->id;
    $reply->name = Auth::user()->fname;
    $reply->email = Auth::user()->email;
    $reply->website = $request->website;
    $reply->body = $request->replyComment;

    $comment->comments()->save($reply);
    Session::flash('success', 'Comment Replyed Successfully');

    return back();
}

我很难找到一个更好的解决方案。

2 个答案:

答案 0 :(得分:2)

不幸的是,不,<a class="nav-link" onclick="menu()">menu</a> <div id="mobile-nav" class="nav-disabled"> <a class="nav-link" onclick="genres()">genres</a> </div> <div id="genres-container" class="nav-disabled"> genres menu </div>目前不支持该功能。

但是,其他人已经遇到了相同的问题,并为此目的编写了反射方法。这是一个来自github上的自由授权项目的示例(通过此相关C# feature request找到)。

用法:

SELECT cast(unix_timestamp(Y_date , "dd/MM/yyyy HH:mm:ss") + 43200 as timestamp) as 
FROM table_a
where instr(`datetime`,'PM') > 0 

union

SELECT cast(unix_timestamp(Y_date , "dd/MM/yyyy HH:mm:ss") as timestamp) as action_time 
FROM table_a
where instr(`datetime`,'AM') > 0 

来源:

答案 1 :(得分:0)

您可以使用表达式解析它:

public string NameOf<TTarget, TProperty>(TTarget dontcare, Expression<Func<TTarget, TProperty>> propertySelector)
{
    var name = propertySelector.Parameters.Single().Name;

    var expression = propertySelector.ToString();
    // remove 'x => '
    expression = Regex.Replace(expression, Regex.Escape(name) + " => ", "");
    // replace 'x.' with '@'
    expression = Regex.Replace(expression, Regex.Escape(name) + @"\.", "");

    return expression;
}
public string NameOf<TProperty>(Expression<Func<TProperty>> propertySelector)
{
    var expression = propertySelector.ToString();
    // remove '() => '
    expression = Regex.Replace(expression, @"\(\) => ", "");
    // replace 'value(...)' with '@'
    expression = Regex.Replace(expression, @"value\([^\)]+\).", "");

    return expression;
}

用法:

var context = new Context();

NameOf(context, x => x.Inner.Inner.Value).Dump(); // Inner.Inner.Value
NameOf(() => context.Inner.Inner.Value).Dump(); // context.Inner.Inner.Value

public class Context
{
    public Context Inner => this;
    public int Value => 1;
}