【问题标题】:Reference to another object in another folder in .net core引用 .net core 中另一个文件夹中的另一个对象
【发布时间】:2019-11-29 20:09:06
【问题描述】:

我已经在 .net mvc 中做过很多次了,所以这应该很容易。

我需要做的就是引用我创建的“Helper”文件夹中的一个方法来清理我的代码。

我在文件夹Helpers中有这个方法。

using Microsoft.AspNetCore.Mvc;
using mocHub2.Models;
using mocHub2.Models.Enterprise;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Threading.Tasks;

namespace mocHub2.Helpers
{
    public class UserIdentity
    {
        private static EnterpriseContext enterpriseDB = new EnterpriseContext();
        private SLGContext db = new SLGContext();

        public List<int> IdentitySearch()
        {
            object newObject = new object();
            bool StoreManagerAccess = false;
            List<int> StoreIdList = new List<int>();
            UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName), IdentityType.SamAccountName, System.Security.Principal.WindowsIdentity.GetCurrent().Name);

            // Check to see if the user is in the Permissions table 
            if (db.Permissions.Any(x => x.Email == user.EmailAddress))
            {
                StoreManagerAccess = true;
                // If exists add StoreId to List<int> StoreIdList
                db.Permissions.Where(x => x.Email == user.EmailAddress).ToList().ForEach(x => { StoreIdList.Add(x.StoreId); });
            }
            var newlist = new HashSet<decimal>();
            var ListFromPermissionsCheck = db.Permissions.Where(x => x.Email == user.EmailAddress).ToList();

            return StoreIdList;
        }
    }
}

我在另一个文件 EnterpriseController.cs 中,我正在尝试引用此方法。

        List<int> storeIdList = UserIdentity.IdentitySearch();

UserIdentity.IdentitySearch() 下有一条红线表示

object reference required for non static field

我可以将我的方法设为静态,但它会给方法中的 db 部分带来相同的错误。

我需要做什么才能引用该方法?

【问题讨论】:

标签: c# asp.net .net model


【解决方案1】:

IdentitySearch 方法不是静态的,所以你不能这样称呼它。 您可以使用 static 关键字将该方法设为静态。

private static SLGContext db = new SLGContext(); //Because IdentitySearch is using that variable, so it has to be static.

public static List<int> IdentitySearch()
//Here the method body

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 2018-10-10
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2012-11-25
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多