使用部分类中的方法

时间:2011-02-21 18:31:28

标签: c# .net partial-classes

所以我有这个方法:

public IList<IndicationProject> GetProjectsForSponsor(int sponsorId)
        {
            IList<IndicationProject> result = new List<IndicationProject>();

            IWspWB_IndicationGetProjectsForEntityResultSet tmpResultSet = ExecWspWB_IndicationGetProjectsForEntity(sponsorId);

            if (tmpResultSet.WspWB_IndicationGetProjectsForEntity1 != null)
            {
                foreach (WspWB_IndicationGetProjectsForEntity1LightDataObject ldo in tmpResultSet.WspWB_IndicationGetProjectsForEntity1)
                {
                    result.Add(
                        new IndicationProject()
                            .Named(NullConvert.From(ldo.Stp_name, null))
                            .IdentifiedBy(NullConvert.From(ldo.Stp_straight_through_processing_id, 0))
                        );
                }
            }

            return result;
        }

包含在此类中:

namespace Web.Data.Indications
{
    public partial class IndicationsDataTier
    {

我想在我的另一个类中使用该方法,如下所示:

IList<IndicationProject> oldList = Web.Data.Indications.IndicationsDataTier.GetProjectsForSponsor(entityId);

但是当我编译时,我得到了这个错误:

错误44非静态字段,方法或属性'Web.Data.Indications.IndicationsDataTier.GetProjectsForSponsor(int)'

需要对象引用

8 个答案:

答案 0 :(得分:3)

您的方法是实例成员,但您将其称为静态。

如果您希望它是静态方法,则应添加static关键字:

public static IList<IndicationProject> GetProjectsForSponsor(int sponsorId)
{
     // ...
}

如果您希望它是一个实例方法,您应该创建(或以其他方式获取对该类型的实例的引用),然后在该实例上调用该方法:

using Web.Data.Indications;

// ...

IndicationsDataTier idt = new IndicationsDataTier();
IList<IndicationProject> oldList = idt.GetProjectsForSponsor(entityId);

答案 1 :(得分:0)

您正在尝试通过类访问该方法,但您需要通过类的实例访问它,因为它不是静态方法。

答案 2 :(得分:0)

指定方法的方法标头不包含static关键字。

答案 3 :(得分:0)

您已将该方法声明为非静态。

您将其作为类方法(MyClass.MyMethod)访问,而不是作为实例方法(myVar.MyMethod)。

将声明更改为static,以便按照您的方式调用它。

答案 4 :(得分:0)

该方法不是静态的,因此您需要实际实例化IndicationsDataTier

答案 5 :(得分:0)

您尚未将该方法声明为静态,因此您需要先创建一个实例。 e.g:

var oldList = new Web.Data.Indications.IndicationsDataTier();

oldList.GetProjectsForSponsor(int sponsorId);

答案 6 :(得分:0)

从阅读方法看,您可以将方法标记为静态。

答案 7 :(得分:0)

如果IndicationsDataTier的构造函数是无参数的,你可以尝试:

IList<IndicationProject> oldList = (new Web.Data.Indications.IndicationsDataTier).GetProjectsForSponsor(entityId);

使用static修饰符..