C#在不创建实例的情况下获取属性值? [英] C# Get property value without creating instance?

查看:32
本文介绍了C#在不创建实例的情况下获取属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不创建实例就可以获得价值吗?

Is it possible to get value without creating an instance ?

我有这门课:

public class MyClass
{
    public string Name{ get{ return "David"; } }

    public MyClass()
    {
    }
}

现在我需要获取值David",而不需要创建 MyClass 的实例.

Now I need get the value "David", without creating instance of MyClass.

推荐答案

真实答案:没有.它是一个 instance 属性,因此您只能在实例上调用它.您应该创建一个实例,或者如其他答案中所示将属性设为静态.

Real answer: no. It's an instance property, so you can only call it on an instance. You should either create an instance, or make the property static as shown in other answers.

请参阅 MSDN 了解有关静态和实例之间区别的更多信息会员.

See MSDN for more information about the difference between static and instance members.

口齿伶俐但答案仍然正确:

不创建实例就可以获得价值吗?

Is it possible to get value without creating an instance ?

是的,但只能通过一些非常可怕的代码,这些代码创建一些 IL 传入 null 作为 this(您不在您的财产中使用),使用 DynamicMethod.示例代码:

Yes, but only via some really horrible code which creates some IL passing in null as this (which you don't use in your property), using a DynamicMethod. Sample code:

// Jon Skeet explicitly disclaims any association with this horrible code.
// THIS CODE IS FOR FUN ONLY. USING IT WILL INCUR WAILING AND GNASHING OF TEETH.
using System;
using System.Reflection.Emit;

public class MyClass
{
    public string Name { get{ return "David"; } }
}


class Test    
{
    static void Main()
    {
        var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
        var dynamicMethod = new DynamicMethod("Ugly", typeof(string), 
                                              Type.EmptyTypes);
        var generator = dynamicMethod.GetILGenerator();
        generator.Emit(OpCodes.Ldnull);
        generator.Emit(OpCodes.Call, method);
        generator.Emit(OpCodes.Ret);
        var ugly = (Func<string>) dynamicMethod.CreateDelegate(
                       typeof(Func<string>));
        Console.WriteLine(ugly());
    }
}

请不要这样做.永远.这太可怕了.它应该被践踏,切成小块,点燃,然后再切碎.不过很有趣,不是吗?;)

Please don't do this. Ever. It's ghastly. It should be trampled on, cut up into little bits, set on fire, then cut up again. Fun though, isn't it? ;)

这是有效的,因为它使用 call 而不是 callvirt.通常,C# 编译器会使用 callvirt 调用 即使它不调用虚拟成员,因为这会免费"进行空引用检查(就 IL 流而言)).像这样的非虚拟调用不会首先检查空值,它只是调用成员.如果您在属性调用中检查了 this,您会发现它为 null.

This works because it's using call instead of callvirt. Normally the C# compiler would use a callvirt call even if it's not calling a virtual member because that gets null reference checking "for free" (as far as the IL stream is concerned). A non-virtual call like this doesn't check for nullity first, it just invokes the member. If you checked this within the property call, you'd find it's null.

正如 Chris Sinclair 所指出的,您可以更简单地使用开放委托实例:

As noted by Chris Sinclair, you can do it more simply using an open delegate instance:

var method = typeof(MyClass).GetProperty("Name").GetGetMethod();
var openDelegate = (Func<MyClass, string>) Delegate.CreateDelegate
    (typeof(Func<MyClass, string>), method);
Console.WriteLine(openDelegate(null));

(但再次请不要这样做!)

(But again, please don't!)

这篇关于C#在不创建实例的情况下获取属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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