在以前的项目中的各个地方使用带有此代码的Copy方法(处理具有相同命名属性但不从公共基类派生或实现公共接口的对象).
新的工作场所,新的代码库 - 现在它在SetValue失败,"对象与目标类型不匹配",即使是非常简单的例子......它在上周工作了....
public static void Copy(object fromObj, object toObj)
{
Type fromObjectType = fromObj.GetType();
Type toObjectType = toObj.GetType();
foreach (System.Reflection.PropertyInfo fromProperty in
fromObjectType.GetProperties())
{
if (fromProperty.CanRead)
{
string propertyName = fromProperty.Name;
Type propertyType = fromProperty.PropertyType;
System.Reflection.PropertyInfo toProperty =
toObjectType.GetProperty(propertyName);
Type toPropertyType = toProperty.PropertyType;
if (toProperty != null && toProperty.CanWrite)
{
object fromValue = fromProperty.GetValue(fromObj,null);
toProperty.SetValue(toProperty,fromValue,null);
}
}
}
}
private class test
{
private int val;
private string desc;
public int Val { get { return val; } …Run Code Online (Sandbox Code Playgroud) 我有一个关于如何确定对象的Nullable属性类型的问题.
具有DateTime属性的ObjectA?CREATEDATE;
当我迭代它的属性,如下面的代码,我如何检查属性是否是Nullable DateTime类型?
谢谢
foreach (PropertyInfo pi in ObjectA.GetType().GetProperties())
{
//do the compare here
}
Run Code Online (Sandbox Code Playgroud) 我使用反射来构造一个以File作为参数的类(ConfigBuilder):
Class myClassType = Class.forName(className);
Class[] types = new Class[] { File.class };
Constructor cons = myClassType.getConstructor(types);
Object[] constructorArgs = new Object[] { myFile };
cb = (ConfigBuilder) cons.newInstance(constructorArgs);
Run Code Online (Sandbox Code Playgroud)
但我得到这个警告:
warning: [unchecked] unchecked call to getConstructor(java.lang.Class<?>...) as a member of the raw type java.lang.Class
Constructor cons = myClassType.getConstructor(types);
Run Code Online (Sandbox Code Playgroud)
显然,似乎getConstructor期望一个泛型类型,所以我尝试了类似的东西:
Class<?>[] types = new Class<?>[] { File.class };
Run Code Online (Sandbox Code Playgroud)
但我得到了同样的警告信息
任何的想法 ?
大卫
我有一些遗留代码,方法foo有700多个重载:
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structA obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structB obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structC obj);
//and 700 similar overloads for foo...
Run Code Online (Sandbox Code Playgroud)
我想通过使用泛型的单个方法公开这些重载方法:
public void callFoo<T>(int len)
where T : new() //ensure an empty constructor so it can be activated
{
T obj = Activator.CreateInstance<T>(); //foo expects obj to be empty, and fills it with data
foo(len, ref obj);
//...do stuff with obj... …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码输出属性值:
string output = String.Empty;
string stringy = "stringy";
int inty = 4;
Foo spong = new Foo() {Name = "spong", NumberOfHams = 8};
foreach (PropertyInfo propertyInfo in stringy.GetType().GetProperties())
{
if (propertyInfo.CanRead) output += propertyInfo.GetValue(stringy, null);
}
Run Code Online (Sandbox Code Playgroud)
如果我为该类型int或Foo复杂类型运行此代码,它可以正常工作.但是如果我为它运行string(如图所示),我在foreach循环内的行上得到以下错误:
System.Reflection.TargetParameterCountException:参数计数不匹配.
有谁知道这意味着什么以及如何避免它?
如果有人问'为什么你要通过字符串的属性进行枚举',最终我希望创建一个泛型类,它将输出传递给它的任何类型的属性(可能是一个字符串......).
在编译.Net程序集时,有什么方法可以访问用于TargetFrameworkVersion和/或TargetFrameworkProfile的值吗?
我正在谈论的值是包含项目文件的值
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OtherStuff>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<OtherStuff>
</PropertyGroup>
<OtherStuff>
</OtherStuff>
</Project>
Run Code Online (Sandbox Code Playgroud)
基本上我想知道编译程序集时框架的目标版本是什么,以及可能的目标框架配置文件.
我不是在谈论当前加载的CLR版本,Environment.Version不是我想要的.
理想情况下,解决方案将使用System.Reflection,但如果我必须采用其他方法,我会.
说我有一个SpaceShip班级,如下:
public class SpaceShip {
public SpaceShip() { }
public SpaceShip(IRocketFuelSource fuelSource) { }
}
Run Code Online (Sandbox Code Playgroud)
我想用于TypeBuilder在运行时创建一个继承自SpaceShip的类型,并为SpaceShip中的每个类型定义一个构造函数.我不需要构造函数实际做任何事情,除了将它们的参数传递给父("传递"构造函数).例如,如果用C#表示,生成的类型看起来像这样:
public class SpaceShipSubClass : SpaceShip {
public SpaceShipSubClass() : base() { }
public SpaceShipSubClass(IRocketFuelSource fuelSource) : base(fuelSource) { }
}
Run Code Online (Sandbox Code Playgroud)
为了使事情复杂化,我实际上并不知道生成类型将继承到哪个类直到运行时(因此我将不得不考虑任意数量的构造函数,可能使用默认参数).
这可能吗?如果我有一个大方向开始,我想我可以弄明白,只是我完全是新手TypeBuilder.
谢谢!
我想在Java程序中定义一个方法拦截器,换句话说我希望在每个方法调用中执行一个行为.此应用程序不在应用程序服务器中执行,因此我无法使用EJB调用拦截器.我在标准Java库中找到了一个很好的Proxy API,但它有限,因为它需要在代理创建中使用一个接口:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
Run Code Online (Sandbox Code Playgroud)
是否有类似的API不强制将Foo.class声明为接口?
我想重新定义一个方法,但避免与之相关的警告.我应该使用undef_method或remove_method这样做吗?
(是的,重新定义方法有点hacky.我这样做是因为我在运行单元测试时有一些我想要使用的memoization,但是当程序本身运行时却没有.)
我想要一个将执行任何外部方法的类,如下所示:
class CrazyClass
{
//other stuff
public AnyReturnType Execute(AnyKindOfMethod Method, object[] ParametersForMethod)
{
//more stuff
return Method(ParametersForMethod) //or something like that
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?是否有代理人采用任何方法签名?