在对象初始化中填充嵌套属性-C#

时间:2018-09-05 08:27:40

标签: c#

这是我的代码:

MyTool tool = new MyTool(new SubTool());
tool.PrintingSystem.Document.Target = 1;
tool.ShowPreview();

我想知道是否可以像这样在初始化中填充PrintingSystem.Document.Target

MyTool tool = new MyTool(new SubTool())
{
   PrintingSystem.Document.Target = 1
};

当前不起作用。

2 个答案:

答案 0 :(得分:3)

与对象初始化程序等效的原始代码为:

MyTool tool = new MyTool(new SubTool())
{
    PrintingSystem = { Document = { Target = 1 } }
};

这只会为PrintingSystem调用 getters ,然后为Document,然后为Target调用 setter -就像您的原始代码。这是对象初始化器相对很少使用的功能,称为<嵌套对象嵌套初始化器。在ECMA C# standard中,第12.7.11.3节:

  

成员初始化程序,用于指定等号后的表达式,其处理方式与对字段或属性的赋值(第12.18.2节)相同。

     

在等号后指定对象初始化器的成员初始化器是 嵌套对象初始化器 ,即嵌入式对象的初始化。而不是为字段或属性分配新值,而是将嵌套对象初始化器中的分配视为对字段或属性成员的分配。嵌套对象初始化程序不能应用于具有值类型的属性,也不能应用于具有值类型的只读字段。

仅在PrintingSystemDocument默认为非空值的情况下才有效-否则,您需要根据fubo的答案设置属性。

答案 1 :(得分:1)

如果属性是一个对象,则引用另一个对象-您必须为每个对象创建一个新实例以避免{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "socialscout": { "root": "projects/socialscout/", "sourceRoot": "projects/socialscout/src", "projectType": "application", "prefix": "app", "schematics": {}, "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/socialscout", "index": "projects/socialscout/src/index.html", "main": "projects/socialscout/src/main.ts", "polyfills": "projects/socialscout/src/polyfills.ts", "tsConfig": "projects/socialscout/tsconfig.app.json", "assets": [ "projects/socialscout/src/favicon.ico", "projects/socialscout/src/assets" ], "styles": [ "projects/socialscout/src/styles.scss", "projects/socialscout/src/custom.scss", "projects/socialscout/src/theme.scss" ], "scripts": [ "node_modules/progressbar.js/dist/progressbar.min.js" ] }, "configurations": { "production": { "fileReplacements": [ { "replace": "projects/socialscout/src/environments/environment.ts", "with": "projects/socialscout/src/environments/environment.prod.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true } } }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "socialscout:build" }, "configurations": { "production": { "browserTarget": "socialscout:build:production" } } }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "socialscout:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "projects/socialscout/src/test.ts", "polyfills": "projects/socialscout/src/polyfills.ts", "tsConfig": "projects/socialscout/tsconfig.spec.json", "karmaConfig": "projects/socialscout/karma.conf.js", "styles": [ "projects/socialscout/src/styles.scss", "projects/socialscout/src/theme.scss" ], "scripts": [], "assets": [ "projects/socialscout/src/favicon.ico", "projects/socialscout/src/assets" ] } }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": [ "projects/socialscout/tsconfig.app.json", "projects/socialscout/tsconfig.spec.json" ], "exclude": [ "**/node_modules/**" ] } } } }, "socialscout-e2e": { "root": "projects/socialscout-e2e/", "projectType": "application", "architect": { "e2e": { "builder": "@angular-devkit/build-angular:protractor", "options": { "protractorConfig": "projects/socialscout-e2e/protractor.conf.js", "devServerTarget": "socialscout:serve" }, "configurations": { "production": { "devServerTarget": "socialscout:serve:production" } } }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": "projects/socialscout-e2e/tsconfig.e2e.json", "exclude": [ "**/node_modules/**" ] } } } }, "defaultProject": "untouchable-app", "schematics": { "@schematics/angular:component": { "styleext": "scss" } } }

NullreferenceException

https://dotnetfiddle.net/1G39Zs