Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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#_Arrays - Fatal编程技术网

C# 如何基于数组填充类?

C# 如何基于数组填充类?,c#,arrays,C#,Arrays,我有一个数据数组,其中可以有1到10个值。根据数组中的值的数量,我想从10个不同的类中选择一个,并将数组的值放在class对象中 这就是我到目前为止所做的 Array[] ar; ar = PopulateTheArray(); int cnt = ar.Count(); Object ob = Activator.CreateInstance(null, "MyObject" + cnt); 有10个像这样的MyObject类 public class MyObject1 { pub

我有一个数据数组,其中可以有1到10个值。根据数组中的值的数量,我想从10个不同的类中选择一个,并将数组的值放在class对象中

这就是我到目前为止所做的

Array[] ar;
ar = PopulateTheArray();
int cnt = ar.Count();
Object ob = Activator.CreateInstance(null, "MyObject" + cnt);
有10个像这样的MyObject类

public class MyObject1
{
    public string Column1 { get; set; }
}
public class MyObject2
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
}
public class MyObject3
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
}
and so on.....

既然对象是动态创建的,我如何循环数组来填充对象?

这个类体系结构看起来确实很奇怪。似乎您有一个约定,即类
MyObjectX
将恰好有X个名为
Column1
-
ColumnX
的属性。我以前从来没有见过这种情况,我也想不到有任何一种情况适合这样做

在任何情况下,我强烈建议您描述您的问题域和当前的体系结构,以便其他人能够评估其适当性,并可能提出替代方案。例如,可能只需要编写一个封装数组(或其他集合)的类:


但我会尽力回答你的问题

您可以这样做:

object ob = ...
object[] ar = ...

for (int i = 0; i < ar.Length; i++)
{
    ob.GetType().GetProperty("Column" + i).SetValue(ob, ar[i], null);
}
objectob=。。。
对象[]ar=。。。
对于(int i=0;i
这个类架构看起来确实很奇怪。似乎您有一个约定,即类
MyObjectX
将恰好有X个名为
Column1
-
ColumnX
的属性。我以前从来没有见过这种情况,我也想不到有任何一种情况适合这样做

在任何情况下,我强烈建议您描述您的问题域和当前的体系结构,以便其他人能够评估其适当性,并可能提出替代方案。例如,可能只需要编写一个封装数组(或其他集合)的类:


但我会尽力回答你的问题

您可以这样做:

object ob = ...
object[] ar = ...

for (int i = 0; i < ar.Length; i++)
{
    ob.GetType().GetProperty("Column" + i).SetValue(ob, ar[i], null);
}
objectob=。。。
对象[]ar=。。。
对于(int i=0;i
如果您有一个抽象基类怎么办

public abstract class MyObjectBase
{
    public abstract void Initialize(params object[] args);
}
然后你的例子变成:

public class MyObject1 : MyObjectBase
{
    public string Column1 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
    }
}
public class MyObject2 : MyObjectBase
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
        this.Column2 = args[1];
    }
}
public class MyObject3 : MyObjectBase
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
        this.Column2 = args[1];
        this.Column3 = args[2];
    }
}
and so on.....
这样称呼:

Array[] ar;
int cnt = ar.Count();
MyObjectBase ob = Activator.CreateInstance(null, "MyObject" + cnt);
ob.Initialize(ar);

如果有一个抽象基类呢

public abstract class MyObjectBase
{
    public abstract void Initialize(params object[] args);
}
然后你的例子变成:

public class MyObject1 : MyObjectBase
{
    public string Column1 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
    }
}
public class MyObject2 : MyObjectBase
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
        this.Column2 = args[1];
    }
}
public class MyObject3 : MyObjectBase
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public override void Initialize(params object[] args)
    {
        this.Column1 = args[0];
        this.Column2 = args[1];
        this.Column3 = args[2];
    }
}
and so on.....
这样称呼:

Array[] ar;
int cnt = ar.Count();
MyObjectBase ob = Activator.CreateInstance(null, "MyObject" + cnt);
ob.Initialize(ar);
试试这个

public interface IMyObject
{

}

public class MyObject1 : IMyObject
{
  public string Column1 { get; set; }
}
public class MyObject2 : IMyObject
{
  public string Column1 { get; set; }
  public string Column2 { get; set; }
}
public class MyObject3 : IMyObject
{
  public string Column1 { get; set; }
  public string Column2 { get; set; }
  public string Column3 { get; set; }
}
接口IMyObject标识您的类。现在使用反射动态创建

var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
var instanceList = new List<IMyObject>();
foreach (Type currentType in assemblyTypes)
{
  if (currentType.GetInterface("IMyObject") == null) 
    continue;

  Console.WriteLine("Found type: {0}", currentType);
  // create instance and add to list
  instanceList.Add(Activator.CreateInstance(currentType) as IMyObject);
}
var assemblyTypes=System.Reflection.Assembly.getExecutionGassembly().GetTypes();
var instanceList=新列表();
foreach(assemblyTypes中的类型currentType)
{
if(currentType.GetInterface(“IMyObject”)==null)
继续;
WriteLine(“找到的类型:{0}”,currentType);
//创建实例并添加到列表
添加(Activator.CreateInstance(currentType)作为IMyObject);
}
希望这会有帮助

试试这个

public interface IMyObject
{

}

public class MyObject1 : IMyObject
{
  public string Column1 { get; set; }
}
public class MyObject2 : IMyObject
{
  public string Column1 { get; set; }
  public string Column2 { get; set; }
}
public class MyObject3 : IMyObject
{
  public string Column1 { get; set; }
  public string Column2 { get; set; }
  public string Column3 { get; set; }
}
接口IMyObject标识您的类。现在使用反射动态创建

var assemblyTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
var instanceList = new List<IMyObject>();
foreach (Type currentType in assemblyTypes)
{
  if (currentType.GetInterface("IMyObject") == null) 
    continue;

  Console.WriteLine("Found type: {0}", currentType);
  // create instance and add to list
  instanceList.Add(Activator.CreateInstance(currentType) as IMyObject);
}
var assemblyTypes=System.Reflection.Assembly.getExecutionGassembly().GetTypes();
var instanceList=新列表();
foreach(assemblyTypes中的类型currentType)
{
if(currentType.GetInterface(“IMyObject”)==null)
继续;
WriteLine(“找到的类型:{0}”,currentType);
//创建实例并添加到列表
添加(Activator.CreateInstance(currentType)作为IMyObject);
}

希望这将有助于

你想完成什么,或者这是一个家庭作业?我希望这是家庭作业,我还没有掌握动态构建这个或如何访问它们。你能澄清一下你想做什么,因为可能还有其他方法来做你想做的事情。我从一个网页上收到数据,数据是一个数组。我需要选择正确的类并将其传递给应用程序的其余部分。对于这个问题,我简化了上面的代码。简言之。。。我需要动态地创建一个对象,将其弹出并传递。你想完成什么,或者这是一个家庭作业?我希望它是家庭作业,我还没有一个关于动态构建这个或者如何访问它们的坚定的把握。你能澄清一下你想要做什么,因为可能有其他的方法来做你正在尝试做的事情。我有来自网页的数据,数据是一个数组。我需要选择正确的类并将其传递给应用程序的其余部分。对于这个问题,我简化了上面的代码。简言之。。。我需要动态地创建一个对象,对其进行压缩并传递它。“params”可能不是必需的,但它是一个非常通用的示例:P“params”可能不是必需的,但它是一个非常通用的示例:P