public class TradeItemDocumentType
{
public EntityIdentificationType tradeItemDocumentIdentification;
public TradeItemType tradeItem;
public ExtensionType extension;
}
我有这样的一个类,请问大家,有什么方法,能用程序自动把该类下的属性都实例化吗?比如:
方法1:
TradeItemDocumentType t = new TradeItemDocumentType();
t.tradeItem = new TradeItemType();
t.tradeItemDocumentIdentification = new EntityIdentificationType();
t.extension = new ExtensionType()可以实例化该类下的所有实例。现在我不能用上面的方法,当我
TradeItemDocumentType t = new TradeItemDocumentType();以后,自动的遍历t下的所有的成员属性,然后自动实例。。
请大虾指点。
{
public EntityIdentificationType tradeItemDocumentIdentification;
public TradeItemType tradeItem;
public ExtensionType extension;
}
我有这样的一个类,请问大家,有什么方法,能用程序自动把该类下的属性都实例化吗?比如:
方法1:
TradeItemDocumentType t = new TradeItemDocumentType();
t.tradeItem = new TradeItemType();
t.tradeItemDocumentIdentification = new EntityIdentificationType();
t.extension = new ExtensionType()可以实例化该类下的所有实例。现在我不能用上面的方法,当我
TradeItemDocumentType t = new TradeItemDocumentType();以后,自动的遍历t下的所有的成员属性,然后自动实例。。
请大虾指点。
System.Reflection.FieldInfo[] fis = t.GetFields();
foreach( FieldInfo fi in fis )
{
Type temp = fi.FieldType;
object otemp = System.Activator.CreateInstance( temp , true );
fi.SetValue( this , otemp ); }
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;namespace ConsoleApplication1
{
public class A
{
public A()
{
Console.WriteLine("at A");
}
}
public class B
{
public B()
{
Console.WriteLine("at B");
}
}
public class C
{
public C()
{
Console.WriteLine("at C");
}
} public class U
{
public U()
{ }
private A a; public A A
{
get { return a; }
set { a = value; }
}
private B b; public B B
{
get { return b; }
set { b = value; }
}
private C c; public C C
{
get { return c; }
set { c = value; }
}
}
class Program
{
static void Main(string[] args)
{
U u = GetU();
} private static U GetU()
{
Type t = typeof(U);
U u = (U)Activator.CreateInstance(t);
foreach (PropertyInfo pi in t.GetProperties())
{
pi.SetValue(u, Activator.CreateInstance(pi.PropertyType), null);
}
return u;
}
}
}
{
public EntityIdentificationType[] tradeItemDocumentIdentification;
public TradeItemType[] tradeItem;
public ExtensionType[] extension;
}public enum ExtensionType
{
ACOUSTO_MAGNETIC_EAS_TAG,
ELECTRO_MAGNETIC_EAS_TAG,
INK_OR_DYE_EAS_TAG,
MICROWAVE_EAS_TAG,
RADIO_FREQUENCY_EAS_TAG,
}其中