【问题标题】:Set static variable to ssis variable - error将静态变量设置为 ssis 变量 - 错误
【发布时间】:2014-02-01 22:38:28
【问题描述】:

我有一个静态变量 -

static readonly String tableName;

我试着这样设置

static readonly String tableName = Dts.Variables["ssisString"].Value.ToString();

我收到一个错误:

An object reference is required for the non-static field, method, or property. 

但是,它的工作原理是这样的:

static String tableName = ""; 

main()
{
     tableName = Dts.Variables["ssisString"].Value.ToString();
}

为什么?

【问题讨论】:

  • @billinkc - 修复了这个问题。但错误仍然存​​在

标签: c# ssis


【解决方案1】:

这不是静态部分,而是readonly 困扰着你。

    static readonly String tableName;

    static ScriptMain()
    {
        // An object reference is required for the non-static field, method, or property 'Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase.Dts.get'
        tableName = Dts.Variables["ssisString"].Value.ToString();
    }

    public void Main()
    {
        // this works
        string local = Dts.Variables["ssisString"].Value.ToString();

        // a static read only field cannot be assinged to (except in a static constructor or a variable initializer)
        tableName = Dts.Variables["ssisString"].Value.ToString();

        Dts.TaskResult = (int)ScriptResults.Success;
    }

关于静态的我记不清的信息是,对于该类的所有实例化,都有一个变量实例。由于只有一个 ScriptMain 实例,您是否从静态变量与实例变量中获得了什么?

无论如何,tableName 的一个实例,您想为它分配一个值。问题是,必须实例化具有您想要使用的值的东西才能提供该值。正如您所演示的,当它在 Main 方法中分配时,这不是问题。

一个问题,因为只读属性只允许您在静态构造函数或变量初始化器中分配一个值......这在我的脑海中又回到了需要一个实例的问题.

一些 .net 的人,请随意使用适当的术语对这个问题进行抨击或改进这个答案。

参考文献

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多