简体   繁体   English

C#中的非空类默认

[英]Non-null class default in C#

If I default(RefType) I get null . 如果我default(RefType)我得到null If I default(ValType) I get a ValType with default -ed properties. 如果我的default(ValType)我得到一个带有default -ed属性的ValType That's fine. 没关系。

But I want a ref-type with val-type default semantics; 但是我想要一个具有val-type default语义的ref-type; particularly for subtypes. 特别是对于亚型。 Is this possible? 这可能吗?

Say, I expose an interface, IExtension . 说,我公开了一个接口IExtension Someone implements it as a class as FooExtension How can I most easily get " default " instances of FooExtension as an "empty" class (and, equally importantly, types derived of FooExtension ) without having to change the implementation? 有人将其作为FooExtensionFooExtension如何最轻松地将FooExtensiondefault ”实例FooExtension为“空”类(以及同样重要的是FooExtension派生的FooExtension ),而不必更改实现?

This is important to me; 这对我很重要; I need to create non- null , "empty" instances of the derived types, so that they can be populated by a dynamic UI. 我需要创建派生类型的非null “空”实例,以便可以由动态UI填充它们。

I'm guessing the answer will be something to the effect of iterating properties via reflection, but perhaps there's a better (read: cleaner) way. 我猜测答案将是通过反射迭代属性的效果,但是也许有更好的方法(阅读:更简洁)。

I would just use structs, but the non-inheritable constraint, paired with the semantics of the types I'm describing being wrong, makes me wary (read: nope) 我只使用结构,但不可继承的约束与我描述的错误类型的语义配对,使我警惕(阅读:nope)

tl;dr: how do I get default classes like default structs, with inheritance supported implicitly. tl; dr:如何获取默认类(如默认结构),并隐式支持继承。

One option is FormatterServices.GetUninitializedObject which allocates an instance without calling any constructors. 一个选项是FormatterServices.GetUninitializedObject ,它分配实例而不调用任何构造函数。 Generally, that is deeply confusing unless you eventually call one of the types constructors or initialize the object fully. 通常,除非最终调用类型构造函数之一或完全初始化对象,否则这将造成极大的混乱。 As the name suggests, it is intended for creation of objects from serialized data but which might not have 0-arity constructors. 顾名思义,它旨在从序列化数据创建对象,但可能没有0 arity构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#泛型:成员的默认非空值 - C# generics: default non-null value for member 将C#非空参数附加到字符串 - C# non-null parameters to be appended to a string C# 中是否有“true or throw”或“non-null or throw”表达式? - Is there a "true or throw" or "non-null or throw" expression in C#? C#List,删除* last非null元素后的所有空值* - C# List, Removing all null values *after* last non-null element 为什么C#在转换后将实例的先前非null成员视为null? - Why does C# consider a previously non-null member of an instance as null after an upcast? 带有外键的数据库模型的 C# 8 非空引用类型 - C# 8 Non-Null Reference Types for Database models with foreign key 有没有办法获取 C# DataTable 中每一列的最后一个非空值并将它们显示为单行? - Is there a way to get the last non-null values of each column in a C# DataTable and display them as a single row? 用于将具有非null值的多个属性收集到集合中的简明C#代码? - Concise C# code for gathering several properties with a non-null value into a collection? 是否可以通知 C# 编译器另一个属性将基于另一个属性非空? - Is it possible to inform the C# compiler that another property will be non-null based on another property? C# 中的可为空变量是否可以从某个时间点为编译器标记为非空? - Can a nullable variable in C# be marked as non-null from some point on for the compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM