Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反射可以用于实例化对象基类属性吗?_C#_Reflection_Inheritance_Jira_Copy Constructor - Fatal编程技术网

C# 反射可以用于实例化对象基类属性吗?

C# 反射可以用于实例化对象基类属性吗?,c#,reflection,inheritance,jira,copy-constructor,C#,Reflection,Inheritance,Jira,Copy Constructor,像这样: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r) { Type t = r.GetType(); foreach (PropertyInfo p in t.GetProperties()) { this

像这样:

    public class remoteStatusCounts : RemoteStatus 
{
    public int statusCount;

    public remoteStatusCounts(RemoteStatus r)
    {
        Type t = r.GetType();
        foreach (PropertyInfo p in t.GetProperties())
        {
            this.property(p) = p.GetValue(); //example pseudocode
        }
    }
}
这个例子有点简单(它来自Jira API-RemoteStatus有4个属性),但假设基类有30个属性。我不想手动设置所有这些值,尤其是当我继承的类只有几个额外属性时

反思似乎暗示着答案

我看到我可以调用基类构造函数(我想?如果我错了请纠正我),但我的基类没有构造函数-它是从jira wsdl派生的

        public remoteStatusCounts(RemoteStatus r) : base(r) { //do stuff }

编辑 我可以想象出两种有效的解决方案:上面概述的一种,以及某种关键字,如
this.baseClass
,它是
类型(baseClass)
,并作为指向
this
的指针进行操作。因此,
this.baseClass.name=“Johnny”
this.name=“Johnny”

出于所有目的,让我们假设基类有一个复制构造函数——也就是说,这是有效的代码:

        public remoteStatusCounts(RemoteStatus r) {
            RemoteStatus mBase = r;
            //do work
        }

edit2 这个问题更多的是一个思考练习,而不是一个实践练习——就我的目的而言,我本可以很容易地做到这一点:(假设我的“基类”可以复制)


是的,您可以做到这一点——不过要注意,您可能会遇到必须单独处理的getter-only属性

您可以使用
Type.GetProperties(BindingsFlags)
重载对其进行筛选

注意:您可能应该研究代码生成(T4将是一个想法,因为它是在VS2008/2010中提供的),因为反射可能会影响运行时,比如执行速度。通过代码生成,您可以轻松地处理这项繁琐的工作,并且仍然具有相同的运行时,例如手动键入

例如:

//extension method somewhere
public static T Cast<T>(this object o)
{
    return (T)o;
}

public remoteStatusCounts(RemoteStatus r)
{
    Type typeR = r.GetType();
    Type typeThis = this.GetType();

    foreach (PropertyInfo p in typeR.GetProperties())
    {
        PropertyInfo thisProperty = typeThis.GetProperty(p.Name);

        MethodInfo castMethod = typeof(ExMethods).GetMethod("Cast").MakeGenericMethod(p.PropertyType);
        var castedObject = castMethod.Invoke(null, new object[] { p.GetValue(r, null) });
        thisProperty.SetValue(this, castedObject, null);
    }
}
//某处的扩展方法
公共静态T强制转换(此对象为o)
{
返回(T)o;
}
公共remoteStatusCounts(RemoteStatus r)
{
Type typeR=r.GetType();
键入typeThis=this.GetType();
foreach(typeR.GetProperties()中的PropertyInfo p)
{
PropertyInfo thisProperty=typeThis.GetProperty(p.Name);
MethodInfo castMethod=typeof(ExMethods).GetMethod(“Cast”).MakeGenericMethod(p.PropertyType);
var castedObject=castMethod.Invoke(null,新对象[]{p.GetValue(r,null)});
SetValue(this,castedObject,null);
}
}

试试。

或代码生成,但我不想走这条路。注意:您的类名应该以大写字母开头<代码>好极了!我以前从未听说过这一点——这肯定会在某个时候派上用场的!怎么用?我无法在对象上调用setValue。。this.setValue(p.Name,r.getValue)不编译。当然,您必须为此获取类型对象,获取与t.GetProperty(Name)对应的属性,并对该对象执行setValue。它的编译方式如下:thisProperty.setValue(this,p.getValue(typeR,null),null);但随后它抛出一个运行时异常“对象与目标类型不匹配”Nope。无法对静态类调用getType()+1。运行时异常-“TargetException未经用户代码处理-对象与目标类型不匹配。”感谢您处理此问题-我真的对反射一无所知。我想这应该是可能的,但我真的不知道。
//extension method somewhere
public static T Cast<T>(this object o)
{
    return (T)o;
}

public remoteStatusCounts(RemoteStatus r)
{
    Type typeR = r.GetType();
    Type typeThis = this.GetType();

    foreach (PropertyInfo p in typeR.GetProperties())
    {
        PropertyInfo thisProperty = typeThis.GetProperty(p.Name);

        MethodInfo castMethod = typeof(ExMethods).GetMethod("Cast").MakeGenericMethod(p.PropertyType);
        var castedObject = castMethod.Invoke(null, new object[] { p.GetValue(r, null) });
        thisProperty.SetValue(this, castedObject, null);
    }
}