Objective-C中是否存在任何反射方法,允许您通过检查对象的公共属性并生成encodeWithCoder:和initWithCoder的通用实现来编写通用NSCoding实现:.
我正在考虑像XStream for Java这样的东西,它允许使用反射对Java对象进行序列化和反序列化的通用方法.更好的方法可能是将属性标记为您想要序列化的东西或者是瞬态的(比如Java中的transient关键字).
我一直在阅读有关Cocoa的Archives and Serializations Programming Guide的文档.我知道您希望对对象的序列化进行一些控制,但它通常是一个对称的过程,并且必须反转为序列化编码的内容以反序列化它似乎很奇怪.我是DRY的信徒(不要重复自己).
iphone reflection serialization objective-c objective-c-runtime
如何通过反射确定对象的类型是由我自己的程序集中的类还是.NET Framework定义的?
我不想在代码中提供我自己的程序集的名称,因为它应该适用于任何程序集和命名空间.
我正在尝试确定我从类型实例上的GetMethod调用获得的MethodInfo对象是由类型还是由它的基类实现的.
例如:
Foo foo = new Foo();
MethodInfo methodInfo = foo.GetType().GetMethod("ToString",BindingFlags|Instance);
Run Code Online (Sandbox Code Playgroud)
ToString方法可以在Foo类中实现.我想知道我是否得到了foo实现?
相关问题
我想在某些方面(或两者)Delegate或MethodInfo有资格获得这个头衔.但是,它们都没有提供我正在寻找的语法上的好处.所以,简而言之,有什么方法可以写下面的内容:
FunctionPointer foo = // whatever, create the function pointer using mechanisms
foo();
Run Code Online (Sandbox Code Playgroud)
我不能使用可靠的委托(即,使用delegate关键字来声明委托类型),因为直到运行时才能知道确切的参数列表.作为参考,这里是我目前在LINQPad中使用的内容,B用户生成的代码将在哪里(因此)Main,因此对我的用户来说非常好,我试图删除.Call:
void Main()
{
A foo = new B();
foo["SomeFuntion"].Call();
}
// Define other methods and classes here
interface IFunction {
void Call();
void Call(params object[] parameters);
}
class A {
private class Function : IFunction {
private MethodInfo _mi;
private A _this;
public Function(A @this, MethodInfo mi) {
_mi = mi;
_this = @this; …Run Code Online (Sandbox Code Playgroud) 我知道这不是完全反思,而是一种.我想创建一个获取变量的调试函数并输出var_dump和变量名.
当然,当程序员写一个函数调用时,他们已经知道了变量的名字,所以他们可以这样写:
debug( $myvar, 'myvar' );Run Code Online (Sandbox Code Playgroud)
但我希望它能够快速轻松地编写,只需要函数名称,变量和vo!
debug( $myvar ); // quicker and easier :)Run Code Online (Sandbox Code Playgroud) 我讨厌拥有一堆"左/右"方法.每次添加或删除属性时,我都必须修复每个方法.而代码本身看起来......错了.
public Foo(Foo other)
{
this.Bar = other.Bar;
this.Baz = other.Baz;
this.Lur = other.Lur;
this.Qux = other.Qux;
this.Xyzzy= other.Xyzzy;
}
Run Code Online (Sandbox Code Playgroud)
实际上,这只是一个展开的循环,它遍历属性,在对象之间复制它们.那么为什么不对这个事实说实话呢?反思救援!
public Foo(IFoo other)
{
foreach (var property in typeof(IFoo).GetProperties())
{
property.SetValue(this, property.GetValue(other, null), null);
}
}
Run Code Online (Sandbox Code Playgroud)
我可能只是试图强迫我从Lua学习到C#的范例,但这个特殊的例子对我来说似乎并不太臭.从这里开始,我开始做一些对字段顺序敏感的更复杂的事情.例如,我不是用一堆几乎相同的if语句来组成字段中的字符串,而是按照所需的顺序迭代它们:
public override string ToString()
{
var toJoin = new List<string>();
foreach (var property in tostringFields)
{
object value = property.GetValue(this, null);
if (value != null)
toJoin.Add(value.ToString());
}
return string.Join(" ", toJoin.ToArray());
}
private static readonly PropertyInfo[] tostringFields =
{
typeof(IFoo).GetProperty("Bar"), …Run Code Online (Sandbox Code Playgroud) 我尝试动态设置Nullable <>属性.
我得到我的房产前:
PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int
Run Code Online (Sandbox Code Playgroud)
我想用反射来设置我的属性
property.SetValue(class,"1256",null);
Run Code Online (Sandbox Code Playgroud)
当我的属性是Nullable <> Generic时,它不起作用.所以我试图找到一种方法来设置我的财产.
要知道我执行的可以为空的<>属性的类型
Nullable.GetUnderlyingType(property.PropertyType)
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
我尝试用我的Nullable <>属性创建一个实例
var nullVar = Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(new Type [] {Nullable.GetUnderlyingType(property.PropertyType)}));
但是nullVar总是空的
我需要获取可以写入的匿名类型的所有属性.
例如:
var person = new {Name = "Person's Name", Age = 25};
Type anonymousType = person.GetType();
var properties = anonymousType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
Run Code Online (Sandbox Code Playgroud)
问题是所有属性都有其CanWrite属性false.对于非匿名类型,返回true.
我也尝试过调用PropertyInfo.GetSetMethod()哪个返回null.
如何检查属性是否可以写入?
编辑: 也许知道一个类型是否是匿名的就足够了.如何使用反射查明某个类型是匿名的?
我正在SQL程序之上创建'通用'包装器,我可以解析所有必需参数的名称和sqltypes,但有没有办法如何获得它的'底层'.NET类型?
我的目标是做一些事情:
SqlParameter param;
object value;
object correctParam = param.GetNETType().GetMethod("Parse",
new Type[] { typeof(string) }).Invoke(value.ToString());
param.Value = correctParam;
Run Code Online (Sandbox Code Playgroud)
GetNETType是我需要的东西.我知道它可以写成param.SqlDbType中的开关,但这是更短的方式,更短的注释代码意味着更低的维护:)
方法也是如此:
我得到了两个PropertyInfo实例或者从它们所在的类GetProperty()或者GetMember()等等中提取的方法(或者可能来自MemberExpression).
我想确定它们是否实际上指的是相同的属性或相同的方法
(propertyOne == propertyTwo)
Run Code Online (Sandbox Code Playgroud)
要么
(methodOne == methodTwo)
Run Code Online (Sandbox Code Playgroud)
显然,这不会真正起作用,您可能正在查看相同的属性,但它可能是从类层次结构的不同级别(通常在这种情况下propertyOne != propertyTwo)中提取的
当然,我可以查看DeclaringType,并重新请求该属性,但是当你开始考虑时,这开始变得有点混乱
在一天结束时,我只是想能够在两个属性或两个方法之间进行智能相等检查,我80%确定上述要点并未涵盖所有边缘情况,而我我可以坐下来,写一堆测试并开始玩,我很清楚我对这些概念实际实现方式的低级知识并不是很好,我希望这是一个已经回答的主题,我只是吮吸搜索.
最好的答案会给我一些实现上述目标的方法,解释已经处理了哪些边缘情况以及为什么:-)
澄清:
从字面上看,我想确保它们是同一个属性,这里有一些例子
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
string Bar { get; set; }
}
typeof(IFoo).GetProperty("Bar")
Run Code Online (Sandbox Code Playgroud)
和
typeof(Foo).GetProperty("Bar")
Run Code Online (Sandbox Code Playgroud)
将返回两个不相等的属性信息:
public class BaseClass
{
public string SomeProperty { get; set ; }
}
public class DerivedClass : BaseClass { }
typeof(BaseClass).GetMethod("SomeProperty")
Run Code Online (Sandbox Code Playgroud)
和 …
reflection ×10
c# ×8
.net ×3
activator ×1
delegates ×1
generics ×1
iphone ×1
methodinfo ×1
nullable ×1
objective-c ×1
overriding ×1
php ×1
refactoring ×1
sql ×1