C# 从类型确定类成员
我有以下情况: 1) 类别:C# 从类型确定类成员,c#,reflection,C#,Reflection,我有以下情况: 1) 类别: 公共C类 { MyTypeObjects的公共列表; 其他类型对象的公共列表; 公共C() { this.ListOfMyTypeObjects=新列表; this.ListOfMyOtherTypeObjects=新列表; } } 2) 接口: public interface IInterface<T> { object CreateObject(T source); } 公共接口接口 { 对象CreateObject(T源); } 3)
公共C类
{
MyTypeObjects的公共列表;
其他类型对象的公共列表;
公共C()
{
this.ListOfMyTypeObjects=新列表;
this.ListOfMyOtherTypeObjects=新列表;
}
}
2) 接口:
public interface IInterface<T>
{
object CreateObject(T source);
}
公共接口接口
{
对象CreateObject(T源);
}
3) 实现类:
public class Implementing<T> : IInterface<T> where T: SomeGeneralType
{
public object CreateObject(T source, int key)
{
//i know T can be of type MyType, but it can also be MyOtherType
C c = (C)storage[key];
//here, i would like to add to one of my members the source object,
//but without specifically know it is called that; in other words, i would like
//to scan c and find if it has any List<T> members; in that case, add to the
//found member (which is unique) the source object and return something,
//doesn't matter what
}
}
public类实现:i接口,其中T:SomeGeneralType
{
公共对象CreateObject(T源,int键)
{
//我知道T可以是MyType,但也可以是MyOtherType
C=(C)存储[键];
//在这里,我想向我的一个成员添加源对象,
//但我不知道这叫什么,换句话说,我想
//扫描c并查找它是否有任何列表成员;在这种情况下,添加到
//找到源对象的成员(唯一)并返回一些内容,
//不管怎样
}
}
4) 对象实例:
IDicionary<int, C> storage = new Dictionary{{1, new C()}};
idicionarystorage=newdictionary{{1,newc()};
如何识别与所需类型对应的成员,以及如何将对象添加到其中?您可以使用类似的反射
using System.Reflection;
PropertyInfo[] properties = Item.GetType().GetProperties();
you can iterate through propeerties
foreach (PropertyInfo p in properties)
{
if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
使用系统反射;
PropertyInfo[]properties=Item.GetType().GetProperties();
您可以遍历属性
foreach(PropertyInfo p in properties)
{
if(p.PropertyType.IsGenericType&&p.PropertyType.GetGenericTypeDefinition()==typeof(字典))
您可以使用反射(搜索通用列表字段):
实际上,我不会对此使用反射,因为类
C
中有固定数量的列表,您可以添加一个返回相应列表的方法:
public class C
{
public List<MyType> ListOfMyTypeObjects;
public List<MyOtherType> ListOfMyOtherTypeObjects;
public C()
{
this.ListOfMyTypeObjects = new List<MyType>;
this.ListOfMyOtherTypeObjects = new List<MyOtherType>;
}
public List<T> GetListFor<T>()
{
if(typeof(T) == typeof(MyType))
return ListOfMyTypeObjects as List<T>;
else if(typeof(T) == typeof(MyOtherType))
return ListOfMyOtherTypeObjects as List<T>;
else
throw new TypeArgumentException("No list properties defined for " +
typeof(T).Name);
}
}
公共C类
{
MyTypeObjects的公共列表;
其他类型对象的公共列表;
公共C()
{
this.ListOfMyTypeObjects=新列表;
this.ListOfMyOtherTypeObjects=新列表;
}
公共列表GetListFor()
{
if(typeof(T)=typeof(MyType))
将ListOfMyTypeObjects作为列表返回;
else if(typeof(T)=typeof(MyOtherType))
将MyotherTypeObjects列表作为列表返回;
其他的
抛出新的TypeArgumentException(“未定义列表属性”+
(T)名称的类型;
}
}
然后你可以这样做:
public object CreateObject(T source, int key)
{
C c = (C)storage[key];
var list = c.GetListFor<T>();
list.Add(source);
return source;
}
public对象CreateObject(T源,int键)
{
C=(C)存储[键];
var list=c.GetListFor();
列表。添加(来源);
返回源;
}
我相信FieldInfo和PropertyInfo之间有一个很好的区别,尽管他们的主题故意分享了很多概念上的相似之处。目前我想不出有什么好的
static IEnumerable<MemberInfo> GetMembers(Type type)
{
var properties = type.GetProperties();
var fields = type.GetFields();
return properties.Cast<MemberInfo>().Concat(fields);
}
static Type GetMemberType(MemberInfo m)
{
return m is PropertyInfo
? (m as PropertyInfo).PropertyType
: (m as FieldInfo).FieldType;
}
静态IEnumerable GetMembers(类型)
{
var properties=type.GetProperties();
var fields=type.GetFields();
返回properties.Cast().Concat(字段);
}
静态类型GetMemberType(MemberInfo m)
{
返回m是PropertyInfo
?(m作为PropertyInfo)。PropertyType
:(m作为FieldInfo)。FieldType;
}
要从泛型“T”中获取类型,可以使用“typeof”:Type givenType=typeof(T);是的,我也会尽量避免反射,但不幸的是,不知道何时会将新列表添加到C,所以我不想修改C.GetListFor的定义。但是,谢谢。当您添加新列表时,您会修改该方法——在同一个类中:)好吧,您拥有我!我将选择此实现,因为它避免了重复flection.还对下一个答案投了赞成票,因为它看起来很优雅。谢谢。最后一条评论:我不得不在GetListFor中的返回中添加as List
public class C
{
public List<MyType> ListOfMyTypeObjects;
public List<MyOtherType> ListOfMyOtherTypeObjects;
public C()
{
this.ListOfMyTypeObjects = new List<MyType>;
this.ListOfMyOtherTypeObjects = new List<MyOtherType>;
}
public List<T> GetListFor<T>()
{
if(typeof(T) == typeof(MyType))
return ListOfMyTypeObjects as List<T>;
else if(typeof(T) == typeof(MyOtherType))
return ListOfMyOtherTypeObjects as List<T>;
else
throw new TypeArgumentException("No list properties defined for " +
typeof(T).Name);
}
}
public object CreateObject(T source, int key)
{
C c = (C)storage[key];
var list = c.GetListFor<T>();
list.Add(source);
return source;
}
static IEnumerable<MemberInfo> GetMembers(Type type)
{
var properties = type.GetProperties();
var fields = type.GetFields();
return properties.Cast<MemberInfo>().Concat(fields);
}
static Type GetMemberType(MemberInfo m)
{
return m is PropertyInfo
? (m as PropertyInfo).PropertyType
: (m as FieldInfo).FieldType;
}