c# - 在 C# 中设置类以初始化公共变量的正确方法是什么?
问题描述
我在 C# 的底部创建了类。此类被 web 服务引用以确定用户访问,如下所示:
[WebMethod]
public List<FAFSA> getFAFSA(string pageID)
{
formValues fv = new formValues();
string personID = fv.personID;
List<FAFSA> lf = new List<FAFSA>();
if (fv.secBlur == "no_secBlur")
{
FAFSA f = new FAFSA();
f.fafsaCheck = "0";
lf.Add(f);
}
...
}
我正在尝试添加两个变量 fafsa 和人员。getSecBlur() 方法从我的数据库中返回 secBlur、fafsa 和人员的所有三个值。那么如何设置这个类,以便 SecBlur 方法只被调用一次,但填充我的所有三个变量,以便它们可以在 web 服务调用中使用?它不会像现在这样工作,因为它说 fafsa 和人员需要是静态的,但是如果我将它们设为静态,那么在 web 服务中它说必须使用实例引用来访问成员。
对不起,如果这不是很好的措辞,但我是新手,仍在努力学习......
public class formValues : System.Web.Services.WebService
{
public string userName = getUserName();
public string firstName = getFirstName();
public string personID = getPersonID();
public int fafsa = 0;
public int staff = 0;
public string secBlur = getSecBlur();
private static string getUserDataString(int ix)
{
string retValue = "";
if (HttpContext.Current.Request.IsAuthenticated)
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
if (ticket != null)
{
string[] userData = { "" };
char[] delimiterChar = { '|' };
userData = ticket.UserData.Split(delimiterChar);
if (userData.Length > 1)
retValue = userData[ix];
else
{
FormsAuthentication.SignOut();
string redirUrl = "/DMC/loginNotFound.html";
HttpContext.Current.Response.Redirect(redirUrl, false);
}
}
}
}
return retValue;
}
private static string getUserName()
{
//This retrieves the person logged into windows/active directory
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
//string[] fullUsername = wp.Identity.Name.Split('\\');
string fullUsername = wp.Identity.Name;
return fullUsername;
}
private static string getFirstName()
{
string firstName = getUserDataString(1);
return firstName;
}
private static string getPersonID()
{
string personID = getUserDataString(0);
return personID;
}
private static string getSecBlur()
{
string secBlur = "no_secBlur";
string mySQL = "exec get_UserAdminStatus @personID";
string cf = System.Configuration.ConfigurationManager.ConnectionStrings["DistrictAssessmentDWConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(cf);
SqlCommand command = new SqlCommand(mySQL, connection);
command.Parameters.AddWithValue("@personID", getUserDataString(0));
connection.Open();
SqlDataReader dr = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
connection.Close();
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["secBlur"].ToString() == "1")
secBlur = "secBlur";
fafsa = Convert.ToInt32(dt.Rows[0]["fafsa"]);
staff = Convert.ToInt32(dt.Rows[0]["staff"]);
}
return secBlur;
}
}
解决方案
如果您为任何类提供静态的公共值,则将调用所谓的“静态”(或类型)构造函数以在完成任何访问之前进行初始化工作:https ://docs.microsoft.com/en-us/dotnet/ csharp/编程指南/类和结构/静态构造函数
另一种进行初始化或定义默认值的常用方法是使用工厂模式。Afaik XNA 中的图形类必须根据您在 X-Box 或 PC 上运行而进行调整,因此它使用工厂模式。
当然,对于 Web(任何东西)来说,变量 Scope 存在整个问题,即使对于静态也是如此。少得多的局部变量。
推荐阅读
- sql - 动态旋转表,但需要非数字值
- python - 优先矩阵
- python - 如何使用 Matplotlib 在 Python 中创建带有滑块的径向条形图,我应该使用什么数据结构?
- python-3.x - 应用相同代码时的不同结果
- puppeteer - 使用 chrome 开发工具协议获取 iframe 中元素的 nodeIds
- azure-ad-b2c - 迁移用户的 Azure AD B2C 成本
- google-bigquery - 获取所有重复记录满足条件的嵌套数据集
- postman - Postman 生成一次静态 guid
- slack - 如何在按钮的帮助下从 Salesforce 案例创建松弛通道
- java - 打印成列时换行长文本