我正在使用反射编写克隆方法.如何使用反射检测属性是索引属性?例如:
public string[] Items
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
我的方法到目前为止:
public static T Clone<T>(T from, List<string> propertiesToIgnore) where T : new()
{
T to = new T();
Type myType = from.GetType();
PropertyInfo[] myProperties = myType.GetProperties();
for (int i = 0; i < myProperties.Length; i++)
{
if (myProperties[i].CanWrite && !propertiesToIgnore.Contains(myProperties[i].Name))
{
myProperties[i].SetValue(to,myProperties[i].GetValue(from,null),null);
}
}
return to;
}
Run Code Online (Sandbox Code Playgroud) public class Address
{
public string ZipCode {get; set;}
}
public class Customer
{
public Address Address {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何通过反射访问"ZipCode"或"Address.ZipCode"?例如:
Typeof(Customer).GetProperty("ZipCode")?
Run Code Online (Sandbox Code Playgroud) 我在运行时使用Reflection进行一些类型分析.如果我有一个MethodInfo实例,我怎么能弄清楚这是一个"真正的"方法还是一个属性的getter/setter方法?如果它是一个属性,我怎样才能找到它的托管PropertyInfo?
我有以下情况:
public interface IPerson { .. }
public class Person : IPerson { .. }
public class User : Person { .. }
Run Code Online (Sandbox Code Playgroud)
现在; 如果我有一个"用户"对象 - 我如何检查这是否使用反射实现IPerson?更确切地说,我有一个可能具有属性SomeUser的对象,它应该是某种类型,实现接口"IPerson".在我的情况下,我实际上有一个用户,但这是我想通过反思检查.我无法弄清楚如何检查属性类型,因为它是一个"用户",但我想检查它是否实现了IPerson ...:
var control = _container.Resolve(objType); // objType is User here
var prop = viewType.GetProperty("SomeUser");
if ((prop != null) && (prop.PropertyType is IPerson))
{ .. }
Run Code Online (Sandbox Code Playgroud)
(请注意,这是我实际情况的简化,但重点应该相同......)
以下是2个代码段
第一个使用ClassLoader类来加载指定的类
ClassLoader cls = ClassLoader.getSystemClassLoader();
Class someClass = cls.loadClass("TargetClass");
第二个使用Class.forName()来加载指定的类
Class cls = Class.forName("TargetClass");
上述方法之间有什么区别.哪一个用于哪个目的?
所以我试图检索我的类中具有特定属性的所有私有方法.当我做
this.GetType().GetMethods()
Run Code Online (Sandbox Code Playgroud)
这将返回18个方法,所有方法都是公共的.所以我尝试修改它以使用如下的绑定标志:
this.GetType().GetMethods(BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)
这导致零结果返回.然后我开始玩游戏,我无法完成任何GetMethods(BindingFlags.x)工作.
this.GetType().GetMethods(BindingFlags.Default);
this.GetType().GetMethods(BindingFlags.Public);
Run Code Online (Sandbox Code Playgroud)
所有这些都返回零结果.我究竟做错了什么?
我正在尝试将一个简单的应用程序移植到Windows 8 Metro(WinRT).似乎缺少一些非常基本的方法.一个基本的例子:Type.GetProperty().它适用于Windows Phone 7,Silverlight和.NET客户端配置文件.我是否必须安装某些东西(例如,一个特殊的库),或者这种方法在.NET metro配置文件中根本不可用?
UPDATE
好的谢谢.现在我用this.GetType().GetTypeInfo().DeclaredProperties.
using System.Reflection;需要有这种GetTypeInfo()扩展方法.
精简版
Type.GetProperties它返回的集合的状态的MSDN文档不保证按字母顺序或声明顺序,但运行一个简单的测试表明,它通常以声明顺序返回.您是否知道具体情况不是这种情况?除此之外,建议的替代方案是什么?
详细版本
我意识到Type.GetProperties状态的MSDN文档:
GetProperties方法不以特定顺序返回属性,例如按字母顺序或声明顺序.您的代码不得依赖于返回属性的顺序,因为该顺序会有所不同.
因此无法保证方法返回的集合将以任何特定方式进行排序.根据一些测试,我发现相反的返回属性按照它们在类型中定义的顺序出现.
例:
class Simple
{
public int FieldB { get; set; }
public string FieldA { get; set; }
public byte FieldC { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Simple Properties:");
foreach (var propInfo in typeof(Simple).GetProperties())
Console.WriteLine("\t{0}", propInfo.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Simple Properties:
FieldB
FieldA
FieldC
Run Code Online (Sandbox Code Playgroud)
一个这样的情况,这只是略有不同的是当有问题的类型的父母也有属性时:
class Parent
{
public int ParentFieldB { get; set; }
public string ParentFieldA { get; set; }
public byte …Run Code Online (Sandbox Code Playgroud) 是否有可能在C#3或4中做到这一点?也许有一些反思?
class Magic
{
[RunBeforeAll]
public void BaseMethod()
{
}
//runs BaseMethod before being executed
public void Method1()
{
}
//runs BaseMethod before being executed
public void Method2()
{
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
有一个替代解决方案,做Magic一个单例并将您的代码放在静态实例的getter上.这就是我做的:
public class Magic
{
private static Magic magic = new Magic();
public static Magic Instance
{
get
{
magic.BaseMethod();
return magic;
}
}
public void BaseMethod()
{
}
//runs BaseMethod before being executed
public void Method1()
{
}
//runs BaseMethod before being executed
public void …Run Code Online (Sandbox Code Playgroud) 只是为了澄清,我使用动态和MakeGenericType.但我不能帮助,但认为有更好的方法来做到这一点.我想要做的是使用Unity创建一个"插件"加载器.我将在发布代码时对其进行解释,以便您了解我正在做的事情.
首先,我将发布插件本身:
[RegisterAction("MyPlugin", typeof(bool), typeof(MyPlugin))]
public class MyPlugin: IStrategy<bool>
{
public IStrategyResult<bool> Execute(ISerializable info = null)
{
bool result;
try
{
// do stuff
result = true;
}
catch (Exception)
{
result = false;
}
return new StrategyResult<bool>
{
Value = result
};
}
}
Run Code Online (Sandbox Code Playgroud)
在这里要注意的事情.首先是RegisterActionAttribute:
[AttributeUsage(AttributeTargets.Class)]
public sealed class RegisterActionAttribute : Attribute
{
public StrategyAction StrategyAction { get; }
public RegisterActionAttribute(string actionName, Type targetType, Type returnType, params string[] depdencies)
{
StrategyAction = new StrategyAction
{
Name = actionName,
StrategyType …Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×8
.net ×3
classloader ×1
clone ×1
generics ×1
getproperty ×1
java ×1
methodinfo ×1
propertyinfo ×1
windows-8 ×1