简体   繁体   English

在 C# 中使用“对象”数据类型,它是否包含成员?

[英]Use of the "Object" data type in C#, does it contain members?

I am a little confused as to the use of the keyword "object" using in C#.我对 C# 中使用的关键字“对象”的使用感到有些困惑。 What can and can't it be used for.什么能用,什么不能用。 Can you access a member?您可以访问会员吗?

For example, I have made a small snippet of code to deserialize a JSON string.例如,我制作了一小段代码来反序列化 JSON 字符串。

public static object DeserializeString(string jsonstring)
{
    // Deserialize the data
    object testObject = JsonConvert.DeserializeObject<object>(jsonstring);
    return testObject;
}

When I use the deserializer to create an object I can not access any memebers.当我使用反序列化器创建对象时,我无法访问任何成员。 I know that this may be a silly question because I haven't defined a class with any members to want to access it.我知道这可能是一个愚蠢的问题,因为我还没有定义任何成员想要访问它的类。 I am trying to avoid making dozens of classes.我试图避免制作数十个课程。

Thanks in advance!提前致谢!

I am a little confused as to the use of the keyword "object" using in C#.我对 C# 中使用的关键字“对象”的使用感到有些困惑。 What can and can't it be used for.什么能用,什么不能用。 Can you access a member?您可以访问会员吗?

C# is by default a statically typed language . C# 默认是静态类型语言 That is, if you have an expression of a particular compile-time type you may access only the members of that type.也就是说,如果您有一个特定编译时类型的表达式,您只能访问该类型的成员。 This is a safety system;这是一个安全系统; it is saying "I only have a guarantee that the members of this type are available, so prevent me from accessing any other members".它说“我只能保证这种类型的成员可用,因此请阻止我访问任何其他成员”。

If you have an expression of type object then you may access the members of object -- ToString , GetType and so on.如果你有一个object类型的表达式,那么你可以访问object的成员—— ToStringGetType等等。

If you want to turn off this safety system, use dynamic instead of object , and you can then call any member you like;如果你想关闭这个安全系统,使用dynamic而不是object ,然后你可以调用任何你喜欢的成员; if you call a member that does not exist, your program will crash.如果你调用一个不存在的成员,你的程序就会崩溃。 When you turn off a safety system the safety system is turned off and you become responsible for guaranteeing safety .当您关闭安全系统时,安全系统也会关闭,您有责任保证安全


CLARIFICATION: Commenter Brian correctly points out that in the specific case of JSON objects, the default behaviour of a bad dynamic member access is to produce null rather than crashing;澄清:评论者 Brian 正确地指出,在 JSON 对象的特定情况下,错误的动态成员访问的默认行为是产生 null 而不是崩溃; of course, much of the time after producing null because a member is unexpectedly missing, the next thing that will happen is a null dereference crash, so there's actually not a whole lot of additional safety margin there, but there is at least some.当然,大部分时间在由于成员意外丢失而产生 null 之后,接下来会发生的是 null 取消引用崩溃,因此实际上没有很多额外的安全余量,但至少有一些。


My question to you was:我问你的问题是:

If you call GetType().ToString() on the object, what do you get?如果你在对象上调用GetType().ToString() ,你会得到什么?

The answer was Newtonsoft.Json.Linq.JObject .答案是Newtonsoft.Json.Linq.JObject So in this case you can access the members of that type like this:因此,在这种情况下,您可以像这样访问该类型的成员:

JObject jo = (JObject)testObject;

(Assuming you have using Newtonsoft.Json.Linq; in your directives.) (假设您在指令中using Newtonsoft.Json.Linq;

Again, your program will crash if the object is not actually of that type.同样,如果对象实际上不是该类型,您的程序将崩溃。

You can do a safe type test with:您可以使用以下方法进行安全类型测试:

if (testObject is JObject jo)
{
  var r = jo.Root;
  ...

I am trying to avoid making dozens of classes.我试图避免制作数十个课程。

C# is a statically-typed language; C# 是一种静态类型语言; it encourages creation of classes.它鼓励创建类。 Again, this is a safety system.再次,这是一个安全系统。 Invest in its correct usage!投资于其正确使用!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM