问题标签 [as-operator]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - How is "as" operator translated when the right-side operand is generic?
I have just posted an answer to this question but I'm not entirely convinced of my answer.There are two things I'm wondering, consider this code:
According to C# Specification 5.0
, there are two different kinds of conversion of as operator
.
If the compile-time type of
E
is notdynamic
, the operationE as T
produces the same result asIf the compile-time type of
E
isdynamic
, unlike the cast operator theas operator
is not dynamically bound (§7.2.2). Therefore the expansion in this case is:
Since, this is invalid because of (Foo<T>)str
I thought it should be translated as:
But the spec says this only happens when the type of E
is dynamic
.
So my questions are:
- Is the compiler translating this expression to a code that is normally invalid?
- When the type of
E
is dynamic why first it castsE
toobject
thenT
while the(T)E
is completely valid?
c# - 如何在 as 运算符中使用变量类型
例如:我有 2 个变量(值)和(属性)我想检查是否可以转换为值?我们不知道变量的类型,如何检查是否可以转换?
样品 1:
样本 2:
c# - IConfiguration.GetSection() 作为属性返回 null
我正在尝试在我的应用程序中检索配置。
我已将 IConfiguration 传递给需要提取一些设置的服务类。
这个类看起来有点像这样:
在调试中,我可以看到 _configuration 确实包含设置,但我的“部分”只是简单地返回为 null。
我知道我可以尝试设置要在启动时创建的 Poco,然后作为依赖项注入,但由于设置,如果可能的话,我宁愿在注入的 IConfiguration 的单独类中进行。
我的 appsettings.json 有这些值:
我的 poco 类如下所示:
c# - Casting 和 As 运算符分别导致被转换的对象和空引用
这个问题几乎纯粹是为了学习目的。使用的环境是 Unity 3D 引擎。
我一直在 C# 中使用 RealProxy 和 MarhalByRefObject 类来装饰我的一个类。具体来说,我使用下面的构造函数创建了一个通用代理类。我正在装饰的类也有一个 SerializableAttribute,同时继承自 MarshalByRefObject。
获取装饰对象,最简单的代码(不带装饰)如下
这就是奇怪的地方。我通过反射检查了类型,它确实和我要装饰的对象是同一类型。然而,令人困惑的部分是,当我正常转换(ClassA)时,我得到一个对装饰对象的引用,而当我使用 as 运算符时,返回一个空引用。
我在测试 Unity v. 2019.1.8f1 的构建时发现了这种行为。我使用的脚本运行时版本和 API 都是 .NET 4.x 等效版本。
如果有人遇到过类似的问题,我很想听听他们的情况,因为不应该发生铸造和操作员行为不同的情况,这可能会导致大量时间和精力的损失。我并不是真的在寻求解决方案,而是寻求可能比我有更好想法或遇到类似问题的人的意见。
注意:如果我只是这样做,则不会发生此行为
编辑:经过进一步调查,我注意到 as 操作员基本上是这样做的
发生的事情是 is 运算符返回 false。
在此处提供重现问题所需的所有代码。
此外,检查每个“铸造”返回的代码:
c# - C# / .NET - operator AS performance/approach
Im working on a e-commerce sort of application.
Entities are:
Product
withList<Expense> Expenses
Expense
withDescription
props which is referring to packaging, transport, administrative costs etc.
I need to have at least 2 types of expenses:
- Relative expense (amount in percentage, calculated off the product price) and
- Absolute expense (amount in numbers)
What i tried doing is having an
abstract class Expense
with props:Id
,Description
.class RelativeExpense : Expense
withAmountInPercentage
props andclass AbsoluteExpense : Expense
withAmount
props.
For calculating expenses I'm having a GetTotalExpenseAmount(Product p)
method:
My question is, is this a good practice? Reason i'm asking is because AS operator is doing casting and i know that can be expensive performance-wise. Plus i will have a Logger
which will print out all expanses for a single product and therefore this foreach
with as
operator will be used again.
Is there any other way i can achieve what i want, but with better/cleaner code and performance? Maybe different modeling? Any ideas how should i approach this?
c# - 强制转换为 C# 中的两种类型之一
如何转换为 C# 中的两种类型之一?
这是我正在尝试做的事情:
但我收到一个错误:
操作员 '??' 不能应用于“Foo”和“Bar”类型的操作数 [Assembly-CSharp] csharp(CS0019)
我在这里想念什么?如果 the也是 type或 type如果is ,我希望specificInstance
是type 。因为,如果不是,我希望是。Foo
instance
Foo
Bar
instance
Bar
instance
Foo
(instance as Foo)
null