实例化“AS” 关键词

发布于 2024-07-14 07:52:01 字数 401 浏览 7 评论 0原文

我最近开始使用 JSON 和 ExtJs 框架,并且在示例中遇到了以下代码。

我们使用以下方式从前端检索信息:

object updatedConfig = JavaScriptConvert.DeserializeObject(Request["dataForm"]);

然后在示例中,他们执行以下操作:

JavaScriptObject jsObj = updatedConfig as JavaScriptObject;

我以前从未见过像这样使用的“as”关键字。 这是否只是将 updateConfig 变量显式装箱为 JavaScriptObject 的另一种形式,还是有什么我不理解的地方?

谢谢

I've recently started working with JSON and the ExtJs framework and I've come across the following code in an example.

we retrieve the information from the frontend using this:

object updatedConfig = JavaScriptConvert.DeserializeObject(Request["dataForm"]);

Then in the example they do the following:

JavaScriptObject jsObj = updatedConfig as JavaScriptObject;

I've never seen the "as" keyword used like that before. Is this just another form of explicitly boxing the updatedConfig variable as a JavaScriptObject or is there something I'm not understanding about this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-07-21 07:52:01

这称为安全强制转换。 它的作用是尝试从一种类型转换为另一种类型,如果转换失败,它将返回 null,而不是抛出 InvalidCastException。

实际上有两个单独的 IL 指令来处理“as”转换和普通静态转换之间的差异。 以下 C# 代码包含两种类型的转换:

using System;

class Program
{
    static void Main()
    {
        Object o = null;

        String s0 = (String)o;
        String s1 = o as String;
    }
}

第一个转换使用 castclass IL 指令,第二个转换使用 isinst 指令。

请参阅转换与在 CLR 中使用“as”关键字< /a> 以获得更详细的解释。

This is known as a safe cast. What is does is it attempts to cast from one type to another and if the cast fails it returns null instead of throwing an InvalidCastException.

There are actually two separate IL instructions to handle the difference between "as" casting and normal static casting. The following C# code contains both types of casting:

using System;

class Program
{
    static void Main()
    {
        Object o = null;

        String s0 = (String)o;
        String s1 = o as String;
    }
}

The first cast uses the castclass IL instruction and the second cast uses the isinst instruction.

Please see Casting vs using the 'as' keyword in the CLR for a more detailed explanation.

思念绕指尖 2024-07-21 07:52:01

as 关键字是一种更安全的转换方式C# 中的对象。

SomeType a = obj as SomeType;

意味着如果 obj 的类型为 SomeType,则 obj 将被强制转换为该类型。 如果 obj 为 null 或不是 SomeType 类型,则 a 将为 null。

The as keyword is a safer way to cast objects in C#.

SomeType a = obj as SomeType;

Means that if obj is of type SomeType, obj will be cast into that type. If obj is null or is not of type SomeType, a will be null.

渔村楼浪 2024-07-21 07:52:01

as 关键字的另一个优点是,如果类型无法强制转换,它将引发编译时异常,而 as (cast) 直到运行时才会中断。

Another advantage of the as keyword is that it will throw a compile time exception if the type can not be cast where as (cast) doesn't break until run-time.

清旖 2024-07-21 07:52:01

另外,重要的是要记住“as”在引用中运行,而不是在对象本身中运行。 这就是为什么它可以返回 null 而不是抛出异常,因为对象将保持完整。 这就是为什么您只能对引用类型执行此操作。

通常情况下,这并不重要,但是如果您实现了转换功能(例如 MSDN)它将不会通过使用 as 运算符来调用。

因此,as 运算符对于“在继承层次结构中上下移动”很有用:如果您有一个 Person 类,您可以执行以下操作:
人 p = new Person();
ojbect o = p 作为对象;
p = o 作为人;
但在所有情况下,内存中的对象都不会以任何方式修改,只会修改对其的引用。

希望有帮助

Also, it's important to remember that "as" operates in the reference not in the object itself. That's why it can return null instead of throwing an exception, because the object will remain intact. And that's why you can only do it on reference types.

Normally it does not matter that much, but if you implement a casting function (like here in MSDN) it will not be invoked by using as operator.

So, the as operator is useful to "move up and down the inheritance hiearchy": If you have a class Person, you can do:
Person p = new Person();
ojbect o = p as object;
p = o as Person;
But in all cases the object in memory won't be modified in any way, just the reference to it.

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文