我们经常被告知我们应该通过为类字段制作getter和setter方法(C#中的属性)来保护封装,而不是将字段暴露给外部世界.
但是很多时候,一个字段只是用来保存一个值,并且不需要任何计算来获取或设置.对于这些,我们都会这样做:
public class Book
{
private string _title;
public string Title
{
get{ return _title; }
set{ _title = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我有一个坦白,我不忍心写所有这些(真的,它不必写它,它不得不看它),所以我去流氓和使用公共领域.
然后是C#3.0,我看到他们添加了自动属性:
public class Book
{
public string Title {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
这更整洁,我很感激它,但是真的,除了创建一个公共领域之外还有什么不同?
public class Book
{
public string Title;
}
Run Code Online (Sandbox Code Playgroud) 我遇到了一些代码
public int MaxHealth =>
Memory[Address].IsValid ?
Memory[Address].Read<int>(Offs.Life.MaxHp) :
0;
Run Code Online (Sandbox Code Playgroud)
现在我对Lambda表达式有点熟悉了.我只是没有看到它以这种方式使用它.
上述陈述与之间的区别是什么?
public int MaxHealth = x ? y:z;
Run Code Online (Sandbox Code Playgroud) 我想dynamic用字符串访问c#属性的值:
dynamic d = new { value1 = "some", value2 = "random", value3 = "value" };
如果我只将"value2"作为字符串,我怎样才能获得d.value2("random")的值?在javascript中,我可以使用d ["value2"]来访问值("随机"),但我不知道如何使用c#和反射来执行此操作.我最接近的是:
d.GetType().GetProperty("value2") ......但我不知道如何从中获得实际价值.
一如既往,感谢您的帮助!
我对理解属性和变量感到困惑
public class ABC()
{
public int A;
public int B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
A和B之间的确切区别是什么?
我对C#中的自动属性有点困惑,例如
public string Forename{ get; set; }
Run Code Online (Sandbox Code Playgroud)
我知道你不需要声明私有变量来保存代码,但是当你不使用任何get或set逻辑时,属性的重点是什么?为什么不用
public string Forename;
Run Code Online (Sandbox Code Playgroud)
我不确定这两个语句之间有什么区别,如果你想要额外的get/set逻辑,我一直认为你使用过属性?
我对使用WPF/Silverlight的MVVM非常熟悉,但这是我第一次尝试使用MVC Web应用程序...这只是我的背景.
我创建了一个名为TestSitesController的控制器,它是从我的实体框架模型中的"站点"模型类(生成读/写操作和视图的模板)中自动生成的.我修改的唯一的东西是3个点,对于某些方法,有一个默认参数Guid id = null.我只是摆脱了"= null"一切正常.这是我改变的一个例子
public ActionResult Delete(Guid id = null)
{
//....
}
Run Code Online (Sandbox Code Playgroud)
这被改为
public ActionResult Delete(Guid id)
{
//....
}
Run Code Online (Sandbox Code Playgroud)
Site模型没有什么特别的SiteId,Abbreviation和DisplayName ...试图让这个问题尽可能简单.好的,所以我运行网站并转到htpp://.../TestSites/,一切都很完美.
我注意到我的所有视图(创建,删除,详细信息和编辑)都使用@model MVCWeb.MyEntities.Site,我现在完全可以使用它; 但在Index.cshtml视图中,我注意到它正在使用
@model IEnumerable<MVCWeb.MyEntities.Site>
Run Code Online (Sandbox Code Playgroud)
这适用于生成的模板,但我想使用"复合视图模型",也许这是我试图混合我的MVVM知识,但如果可能的话,将坚持使用.在我看来,复合视图模型只是一个特定于一个视图的模型,该视图由一个或多个实体模型以及像SelectedSiteId等其他属性组成.
所以我创建了一个非常简单的ViewModel,名为TestSitesViewModel
public class TestSitesViewModel
{
//Eventually this will be added to a base ViewModel to get rid
//of the ViewBag dependencies
[Display(Name = "Web Page Title")]
public string WebPageTitle;
public IEnumerable<MVCWeb.MyEntities.Site> Sites;
//Other Entities, IEnumberables, or properties go here
//Not important for this example
}
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中添加了一个名为IndexWithViewModel的动作方法
public ActionResult …Run Code Online (Sandbox Code Playgroud) 如何使用反射获取继承的属性值?我尝试BindingFlags但仍然触发NullReferenceException
object val = targetObject.GetType().GetProperty("position", BindingFlags.FlattenHierarchy).GetValue(targetObject, null);
Run Code Online (Sandbox Code Playgroud)
position 是一个公共财产,具有声明的价值.
编辑:
class myParent
{
public float[] position;
public myParent()
{
this.position = new float[] { 1, 2, 3 };
}
}
class myChild : myParent
{
public myChild() : base() { }
}
myChild obj = new myChild();
PropertyInfo p = obj.GetType().GetProperty("position", BindingFlags.Instance | BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)
我尝试了几种与BindingFlags的组合,但p总是为null :(,
示例代码(替代代码如下),
// person.cs
using System;
class Person
{
private string myName ="N/A";
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
public override string ToString()
{
return "Name = " + Name;
}
public static void Main()
{
Person person = new Person();
Console.WriteLine("Person details - {0}", person);
person.Name = "Joe";
Console.WriteLine("Person details - {0}", person);
}
}
Run Code Online (Sandbox Code Playgroud)
我们不能直接写,myName从私有变为public,没有要求声明另一个公共变量Name而不需要使用get和set?
替代代码
// person.cs
using …Run Code Online (Sandbox Code Playgroud) 可能重复:
C#中属性和字段之间的差异
我认为基本属性({ get; set; })与公共字段相同,只有能够在不破坏二进制兼容性的情况下更改它们的优点.按照我的答案/sf/answers/611471241/,我发现属性也有缺点.如果它们是值类型,则无法通过引用访问它们.为什么会这样,还有其他什么差异?
c# ×10
properties ×3
field ×2
.net-4.5 ×1
c#-3.0 ×1
c#-6.0 ×1
class ×1
dynamic ×1
razor-2 ×1
reflection ×1