小小辉少 2015-08-23 14:25 采纳率: 50%
浏览 2675

无法显式调用运算符或访问器 无法对属性或索引器赋值

困扰了好久了。c#反汇编回来的程序报了2个错误,

 this.msc.add_Error(new DScriptControlSource_ErrorEventHandler(OnError));
 this.msc.add_Timeout(new DScriptControlSource_TimeoutEventHandler(OnTimeout));

提示:“MSScriptControl.DScriptControlSource_Event.Error.add”: 无法显式调用运算符或访问器

改成这样:

 this.msc.Error += new DScriptControlSource_ErrorEventHandler(OnError);

也一样不行,提示:无法对属性或索引器“MSScriptControl.IScriptControl.Error”赋值 -- 它是只读的

完整代码如下

 using MSScriptControl;
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;

    public class ScriptEngine : IDisposable
    {
        public ScriptControl msc;

        public event RunErrorHandler RunError;

        public event RunTimeoutHandler RunTimeout;

        public ScriptEngine() : this(ScriptLanguage.VBscript)
        {
        }

        public ScriptEngine(ScriptLanguage language)
        {
            this.msc = new ScriptControlClass();
            this.msc.UseSafeSubset = true;
            this.msc.Language = language.ToString();
            this.msc.add_Error(new DScriptControlSource_ErrorEventHandler(OnError));
            this.msc.add_Timeout(new DScriptControlSource_TimeoutEventHandler(OnTimeout));

        }

        public void Dispose()
        {
            Marshal.ReleaseComObject(this.msc);
        }

        public object Eval(string expression, string codeBody)
        {
            this.msc.AddCode(codeBody);
            return this.msc.Eval(expression);
        }

        public object Eval(ScriptLanguage language, string expression, string codeBody)
        {
            if (this.Language != language)
            {
                this.Language = language;
            }
            return this.Eval(expression, codeBody);
        }

        private void OnError()
        {
            if (this.RunError != null)
            {
                this.RunError();
            }
        }

        private void OnTimeout()
        {
            if (this.RunTimeout != null)
            {
                this.RunTimeout();
            }
        }

        public void Reset()
        {
            this.msc.Reset();
        }

        public object Run(string mainFunctionName, object[] parameters, string codeBody)
        {
            this.msc.AddCode(codeBody);
            return this.msc.Run(mainFunctionName, ref parameters);
        }

        public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody)
        {
            if (this.Language != language)
            {
                this.Language = language;
            }
            return this.Run(mainFunctionName, parameters, codeBody);
        }

        public bool AllowUI
        {
            get
            {
                return this.msc.AllowUI;
            }
            set
            {
                this.msc.AllowUI = value;
            }
        }

        public ScriptLanguage Language
        {
            get
            {
                return (ScriptLanguage) Enum.Parse(typeof(ScriptLanguage), this.msc.Language, false);
            }
            set
            {
                this.msc.Language = value.ToString();
            }
        }

        public int Timeout
        {
            get
            {
                return 0;
            }
        }

        public bool UseSafeSubset
        {
            get
            {
                return this.msc.UseSafeSubset;
            }
            set
            {
                this.msc.UseSafeSubset = true;
            }
        }
    }

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-08-23 14:37
    关注

    参考:
    http://blog.chdz1.com/NETjs/158.html

    重新引用下ScriptControl

       ((DScriptControlSource_Event)msc).Error += new DScriptControlSource_ErrorEventHandler(ScriptEngine_Error);
                ((DScriptControlSource_Event)msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services