MatchEvaluator给出“无法使用Lambda表达式...".错误 [英] MatchEvaluator gives "Cannot use a lambda expression..." error

查看:114
本文介绍了MatchEvaluator给出“无法使用Lambda表达式...".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改几个文件的内容.我有此正则表达式的形式,可以直接从按钮的click事件中调用它.

I'm modifying the contents of several files. I have this Regex in a form and it's called directly from a button's click event.

public partial class MainForm : Form
{
    private void UpdateButton_Click(object sender, EventArgs e)
    {
        // code

        if (!OldURLBox.Text.IsEmpty() && !NewURLBox.Text.IsEmpty())
        {
            Regex patternURL = new Regex(string.Format("s:\\d+:\\\\\"((.(?!s:\\d+))*?){0}(.*?)\\\\\";", OldURL));
            content = patternURL.Replace(content, delegate(Match m) // works fine
            {
                var prefix = m.Groups[1].Value;
                var postfix = m.Groups[3].Value;
                var length_prefix = prefix.Replace("\\n", "$").Length;
                var length_postfix = postfix.Replace("\\n", "$").Length;
                var length_total = length_prefix + NewURL.Length + length_postfix;
                return string.Format("s:{0}:\\\"{1}{2}{3}\\\";", length_total, prefix, NewURL, postfix);
            });
        }

        // code
    }
}

这很好.我已将此代码(用正则表达式替换)移至另一个项目,并收到以下错误:

This is working fine. I've moved this code (regex replace) to another project and getting the following error:

在未先将其转换为委托或表达式树类型的情况下,不能将lambda表达式用作动态调度的操作的参数

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

我没有在事件函数中直接调用正则表达式替换,我想这就是导致错误的原因.我不知道该如何解决.检查以下代码.这是我正在使用的结构.省略了无关紧要的部分.

I'm not calling the regex replace directly inside an event function and I guess that's what causing the error. I don't know how to fix it. Examine the following code. This is the structure I'm using. Omitted irrevelant parts.

public partial class ProgressForm : Form
{
    private void ProgressForm_Load(object sender, EventArgs e)
    {
        // when run without delay, it freezes, waits for targz extraction
        System.Timers.Timer timer = new System.Timers.Timer(500);
        timer.Elapsed += (sender2, e2) => OnTimedEvent(sender2, e2);
        timer.AutoReset = false;
        timer.Enabled = true;
    }

    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        this.BeginInvoke((MethodInvoker)delegate
        {
            if (File.Exists(Template.File.ArchiveTarGz))
            {
                // code; extract files from archive

                ModifyFiles();

                // code; repack files
            }
        });
    }

    private void ModifyFiles()
    {
        // code

        Regex patternURL = new Regex(string.Format("s:\\d+:\\\\\"((.(?!s:\\d+))*?){0}(.*?)\\\\\";", Template.Website.URL));
        DBText = patternURL.Replace(DBText, delegate(Match m) // Error: Cannot use a lambda expression as an argument ...
        {
           var prefix = m.Groups[1].Value;
           var postfix = m.Groups[3].Value;
           var length_prefix = prefix.Replace("\\n", "$").Length;
           var length_postfix = postfix.Replace("\\n", "$").Length;
           var length_total = length_prefix + UserInput.Website.URL.Length + length_postfix;
           return string.Format("s:{0}:\\\"{1}{2}{3}\\\";", length_total, prefix, UserInput.Website.URL, postfix);
        });

        // code
    }
}

这可能是重复的,因为还有其他确切的错误问题,但是我找不到可以在我的代码中实现的东西.

有人可以将标题更改为更合适的标题.我不希望标题是错误的,因为与此相关的几个问题已经存在.

推荐答案

您正在替换DBText的内容. Regex.replace期望将字符串作为第一个参数.我猜DBText是动态类型,在这种特殊情况下会使编译器感到困惑.

You are replacing the contents of DBText. Regex.replace expectes a string as first argument. I am guessing DBText is of type dynamic, which confuses the compiler in this particular case.

您只需将DbText强制转换为字符串,就可以了

You can simply cast DbText to string and you should be fine

patternURL.Replace((string)DBText, (...)

这篇关于MatchEvaluator给出“无法使用Lambda表达式...".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆