我有一个类,让我们用名为Name的属性调用它.使用该属性,我有一个与之关联的属性.
public class Book
{
[Author("AuthorName")]
public string Name
{
get; private set;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的main方法中,我正在使用反射并希望为每个属性获取每个属性的键值对.所以在这个例子中,我希望看到属性名称为"Author",属性值为"AuthorName".
问题:如何使用Reflection获取属性的属性名称和值?
我想尝试一下Swift lang,我想知道如何将以下Objective-C转换为Swift:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass: UIPickerView.class]) {
//your touch was in a uipickerview ... do whatever you have to do
}
}
Run Code Online (Sandbox Code Playgroud)
更具体地说,我需要知道如何isKindOfClass在新语法中使用.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
???
if ??? {
// your touch was in a uipickerview ...
}
}
Run Code Online (Sandbox Code Playgroud) 鉴于这门课程
class Foo
{
// Want to find _bar with reflection
[SomeAttribute]
private string _bar;
public string BigBar
{
get { return this._bar; }
}
}
Run Code Online (Sandbox Code Playgroud)
我想找到我将用属性标记的私有项_bar.那可能吗?
我已经用我寻找属性的属性做了这个,但从来没有私有成员字段.
获取私有字段需要设置哪些绑定标志?
我想知道如何检查变量是否是一个类(不是实例!).
我已经尝试使用该函数isinstance(object, class_or_type_or_tuple)来执行此操作,但我不知道类将具有什么类型.
例如,在以下代码中
class Foo: pass
isinstance(Foo, **???**) # i want to make this return True.
Run Code Online (Sandbox Code Playgroud)
我试图取代" class"与??? ,但我意识到这class是python中的关键字.
假设以下类型定义:
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
Run Code Online (Sandbox Code Playgroud)
当只有受损的类型可用时,如何确定类型是否Foo实现了通用接口IBar<T>?
我经常听到反射的糟糕程度.虽然我一般都避免反思,很少发现没有它就无法解决问题的情况,我想知道......
对于那些在应用程序中使用过反射的人,你是否测量过性能命中率,是否真的如此糟糕?
我有一个类型,t我想获得具有该属性的公共属性列表MyAttribute.该属性标有AllowMultiple = false,如下所示:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
Run Code Online (Sandbox Code Playgroud)
目前我拥有的是这个,但我认为有更好的方法:
foreach (PropertyInfo prop in t.GetProperties())
{
object[] attributes = prop.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Length == 1)
{
//Property with my custom attribute
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能改善这个?我很抱歉,如果这是重复的,那里有大量的反思线程......似乎这是一个非常热门的话题.
就像标题所说:反射可以为您提供当前正在执行的方法的名称.
由于海森堡的问题,我倾向于不猜.如何在不改变当前方法的情况下调用一种方法来告诉您当前的方法?但是我希望有人可以在那里证明我的错.
更新:
最终结果
我了解了MethodBase.GetCurrentMethod().我还了解到,我不仅可以创建堆栈跟踪,而且如果需要,我只能创建我需要的确切帧.
要在属性中使用它,只需使用.Substring(4)删除'set_'或'get_'.
这段代码:
Type.GetType("namespace.a.b.ClassName")
Run Code Online (Sandbox Code Playgroud)
回报null.
我在使用中:
using namespace.a.b;
Run Code Online (Sandbox Code Playgroud)
更新:
类型存在,它在不同的类库中,我需要通过字符串名称来获取它.
这让我很难过.我试图为Noda Time优化一些测试,我们在那里进行了一些类型的初始化程序检查.在将所有内容加载到新内容之前,我想我会发现一个类型是否具有类型初始化程序(静态构造函数或带有初始化程序的静态变量)AppDomain.令我惊讶的是,NullReferenceException尽管我的代码中没有空值,但对此进行了一次小测试.它只有当没有调试信息编译抛出异常.
这是一个简短但完整的程序来演示问题:
using System;
class Test
{
static Test() {}
static void Main()
{
var cctor = typeof(Test).TypeInitializer;
Console.WriteLine("Got initializer? {0}", cctor != null);
}
}
Run Code Online (Sandbox Code Playgroud)
以及编译和输出的记录:
c:\Users\Jon\Test>csc Test.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17626
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
c:\Users\Jon\Test>test
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at System.RuntimeType.GetConstructorImpl(BindingFlags bindingAttr, Binder bin
der, …Run Code Online (Sandbox Code Playgroud) .net reflection typeinitializer nullreferenceexception .net-4.5
reflection ×10
.net ×6
c# ×6
.net-4.5 ×1
ios ×1
performance ×1
propertyinfo ×1
python ×1
swift ×1