[C#]通过方法访问属性? [英] [C#] accessing property through method ?

查看:222
本文介绍了[C#]通过方法访问属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我无法理解这是如何工作的:



 使用系统; 
使用 System.IO;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 CSharp_Practicing
{

class 程序
{
静态 void Main( string [] args)
{
Exception ex = new 异常();
Console.WriteLine(ex.GetType()。Name); // 此行

}
}

}





有人可以解释一下吗?谢谢!



我尝试过:



在这里提出问题CodeProject.com

解决方案

您好,



您可以访问该属性,因为该方法返回一个对象键入键入,表示异常类型。



我会更具体:



ex Exception ,因此您可以通过调用<$ c $来访问 GetType()方法C> ex.GetType()。该版本的类型为 System.Type 链接)表示类型异常。该类型还有一些其他方法/属性,因此您可以使用点运算符访问它们,就像之前使用 ex 一样。所以你可以写(我会在这里用括号括起来用于教学目的,但它们不是必需的。)



(ex .GetType())。名称
// - [1] -------- [2] -





[1]:你得到 System.Type的实例 Exception对象的实例 ex (有关 GetType()方法)。

[2]:现在你有一个 System.Type 类型对象的实例(实际上无论你如何得到它,现在你可以实际使用它 ):访问该实例的Name属性。所以你得到System.Exception(我相信),因为你询问了Exception对象的System.Type,而Type的名字是Exception。



那是什么你的代码说。



希望这有帮助。


文档中描述了所有内容:Object.GetType Method(System) [ ^ ]。

例外 [< a href =https://msdn.microsoft.com/en-us/library/system.exception(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]是一个从继承的类(就像最后的其他内容一样)对象 [ ^ ] class。该类有一个名为 GetType 的方法[< a href =https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ],返回类型的对象[ ^ ]类,有一个属性名称 [ ^ ] ...

Hi. I cant understand how this is working:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_Practicing
{

    class Program
    {
        static void Main(string[] args)
        {
            Exception ex = new Exception();
            Console.WriteLine(ex.GetType().Name); // THIS LINE 

        }
    }
   
}



Could someone explain me ? Thanks!

What I have tried:

Asking question here in CodeProject.com

解决方案

Hi,

You can access that property because the method returns an object of type Type that represents the exception type.

I'm going to be more specific:

ex is an Exception, so you can access the GetType() method by calling ex.GetType(). That stamement is of type System.Type (link) that represents the type Exception. That type also has some other methods/properties, so you can access them by using the dot operator as you previously did with ex. So you can write (I'll split it here using parenthesis for didactic purposes, but they are not necessary).

(ex.GetType()).Name
//--[1]--------[2]--



[1]: you get the instance of System.Type of the instance of the Exception object ex (see Richard MacCutchan's answer for more details about the GetType() method).
[2]: now you have an instance of an object of type System.Type (actually it does not matter how you got it, now you can actually use it): access the Name property of that instance. So you get "System.Exception" (I believe) because you asked the System.Type of an Exception object and that Type's name is "Exception".

That's what your code says.

Hope this helps.


It is all described in the documentation: Object.GetType Method (System)[^].


Exception[^] is a class that inherits (like anything else at the end) from Object[^] class. That class has a method called GetType[^], that returns a object of Type[^] class, that has a property Name[^]...


这篇关于[C#]通过方法访问属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆