【对象初始化的完整过程(C#)】教程文章相关的互联网学习教程文章

c# – 为什么用unchecked(Environment.TickCount * 31)初始化一个新的Random()?【代码】

我发现了一个Random实例的初始化:var random = new Random(unchecked(Environment.TickCount * 31));为什么不简单地使用新的Random()?解决方法:关键字unchecked可防止在计算Environment.TickCount * 31整数溢出时抛出异常. 得到的计算本质上是一个随机整数(它抛弃了一堆高阶位),用于为随机数生成器播种. 请注意,Reference Source for Random将此代码作为其无参数构造函数:public Random() : this(Environment.TickCount) {}

C#嵌套初始化陌生感【代码】

在这些初始化语句编译的前提下List<int> l = new List<int> { 1, 2, 3 }; Dictionary<int, int> d = new Dictionary<int, int> { [1] = 11, [2] = 22 }; Foo f = new Foo { Bar = new List<int>() };这不会List<int> l = { 1, 2, 3 }; Dictionary<int, int> d = { [1] = 11, [2] = 22 }; Foo f = { Bar = new List<int>() };我有一个关于嵌套初始化的问题.鉴于以下课程public class Foo {public List<int> Bar { get; set; } = new...

c# – IExtensionConfigProvider没有初始化或绑定Microsoft.Azure.WebJobs v3【代码】

我们使用与IExtensionConfigProvider的参数绑定在Azure函数(netstandard20上的v2)中使用依赖注入.将Microsoft.NET.Sdk.Functions从1.0.13升级到1.0.19(强制将Microsoft.Azure.Webjobs.Host升级到v3)之后,这不再起作用了.我无法在IExtensionConfigProvider.Initialize函数中找到断点.相同版本的Functions SDK适用于具有目标框架net462的示例项目,它使用Microsoft.Azure.WebJobs v2. 这是它在运行时给出的错误:Error indexing metho...

c# – 如何使用特定的Url地址初始化wcf客户端?【代码】

我记得ASMX有一个简单的解决方案:MyAsmxServiceClient serviceClient = new MyAsmxServiceClient("http://myServiceLocation/myService.asmx");如何用WCF实现相同的目标?解决方法:这通常在app.config / web.config中完成:<system.serviceModel><client><endpointaddress="http://myServiceLocation/myService.asmx"binding="basicHttpBinding"contract="IMyServiceContract" /></client> </system.serviceModel>如果您愿意,还是...

c# – 你可以在他们自己的初始化行中使用变量(字典中的tryGetOrElse)吗?【代码】

请考虑以下代码:private Dictionary<RobotSettings, Trader> createTradersFor(IEnumerable<RobotSettings> settings) {var traderSet = new Dictionary<Tuple<IGateway, IBroker>, Trader>();return settings.ToDictionary(s => s, s =>{var key = Tuple.Create(s.gateway, s.broker);Trader trader = traderSet.TryGetValue(key, out trader)? trader: traderSet[key] = new Trader(s.gateway, s.broker);return trader;}); }我...

c# – Azure Worker角色 – 使用OnStart()与构造函数进行一次初始化

阅读各种文档和博客,听起来像OnStart是在角色开始接收流量之前初始化您需要的对象和事物的地方.但是,我不清楚是否存在使用worker角色类的相同实例可以多次调用OnStart的情况? 例如,我有一个注入容器和一个应该只创建一次的数据库连接.我需要确保角色无法停止并再次启动,将所有当前对象保留在内存中.在这种情况下,使用worker角色构造函数初始化对象似乎更好.解决方法:OnStart()(RoleEntryPoint类的成员,以及需要覆盖的方法)仅在角色...

C#中具有复杂初始化的静态字符串变量【代码】

我有一个带有静态字符串变量的类,它具有一些复杂的初始化(我不能将它设置为等于引用的字符串,如“what”).我需要运行几行代码来实际创建值.一旦设置,其值将不会改变.它目前设置为一个属性,只在第一次调用get时设置.class MyClass {private static string _myString = "";public static string MyString{get{if(_myString == ""){// use an object "obj" here to create the valueMyObject obj = new MyObject();obj.someSetupHere(...

c# – 使用集合初始值设定项初始化WebControl对象的Attributes属性【代码】

我想初始化WebControl对象,内联,但对于某些字段,这有点棘手.例如,当我尝试初始化TextBox对象的Attributes属性时,如下所示:using System.Web.UI.WebControls; Panel panel = new Panel() { Controls = { new TextBox() { Attributes = { { "key", "value" } } } } };我收到错误:Cannot initialize type ‘07001’ with a collectioninitializer because it does not implement‘System.Collections.IEnumerable’知道在这种情况下...

c# – 考虑使用IDbContextFactory在设计时覆盖DbContext的初始化【代码】

在ASP.NET Core 1.0.1项目上,使用Entity Framework Core和ASP.NET Identity,我有以下上下文:public class Context : IdentityDbContext<User, Role, Int32, UserClaim, UserRole, UserLogin, RoleClaim, UserToken> { public Context(DbContextOptions options) : base(options) { }protected override void OnModelCreating(ModelBuilder builder) {base.OnModelCreating(builder);} }以下实体:public class User : IdentityUse...

c# – Azure SDK 1.4 – WIF – 尝试初始化默认应用程序域时,C模块无法加载【代码】

我有一个旧的azure应用程序,它运行在azure 1.2 SDK上,依赖于WIF进行身份验证. 我最近一直在尝试将其更新到最新的SDK(1.4). 应用程序在Azure上编译和运行,包括使用我的STS进行身份验证.但是,网站上的某些页面不会加载抛出以下错误.Unable to find assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.[SerializationException: Unable to find assembly 'Microsoft.IdentityMo...

c# – System.Drawing.Color Struct如何初始化颜色?【代码】

我需要创建一个包含一些方法和一长串常量的类型. 经过一番研究后,我想我想采用System.Drawing.Color结构采用的相同方法.但是,查看此结构的源(从元数据生成)给出了类似下面的内容.public byte A { get; } public static Color AliceBlue { get; } public static Color AntiqueWhite { get; } public static Color Aqua { get; } public static Color Aquamarine { get; } public static Color Azure { get; } public byte B { get; ...

c# – NHibernate错误 – “无法初始化集合”【代码】

我有多对一的映射工作正常,但位置和location_times之间的一对多关系一直给我一个错误. 我一直收到这个错误: 在这行代码上: 映射看起来像这样: 地点:public virtual IList<LocationTimes> LocationTimes { get; set; }public virtual int locationID { get; set; }public virtual IList<LocationTimes> LocationTimes { get; set; }public Location(){LocationTimes = new List<LocationTimes>();}位置图:public class Locatio...

c# – 使用IL中的Initobj操作码初始化可空类型【代码】

为什么C#编译器不能简单地调用默认的隐式无参数.ctor而不是forbj为null赋值的可空值类型? 让我们说我们有代码:Nullable<int> ex1 = new Nullable<int>(); Nullable<int> ex2 = null; Nullable<int> ex3 = new Nullable<int>(10); 这两行的IL输出如下所示: 它只调用.ctor作为最后一个语句,为什么我们不能拥有两个带有归零字段的第一个语句的实例?解决方法:Why doesn’t C# compiler simply call default implicit parameterless...

c# – 如何防止用户错误地初始化变量?【代码】

我正在研究一个名为PByte的类,它应该表示一个介于32和126之间的int值.(PByte = Printable Byte.)现在我想阻止类的用户错误地初始化一个对象,但我不想抛出异常,我只是想让Visual Studio不能编译,就像你尝试这样做时发生的那样:byte b = 256;sealed class PByte : IEquatable<PByte>, IComparable, IComparable<PByte> {public PByte(int value){/* if (value < 32 || value > 126)throw new ArgumentException("\"" + value + "\" ...

c# – 自动属性初始化IL指令顺序

我想在auto属性上设置默认值,用Fody做一些IL编织. 据我所知,初始化只是一个语法糖,它在构造函数中设置了支持字段.所以我认为使用从最后一个属性的初始化结束到stfld指令的指令创建默认值,该指令设置当前属性的支持字段. 但是,这假设初始化始终是构造函数中的第一件事.这是正确的假设吗?是否有任何边缘情况需要考虑,例如优化?解决方法:我找到了这个名为C#中的Upcoming Features的pdf文件,它描述了C#6的新语言功能. 这是关于auto属...