一个反射的问题

gaojinhellonet 2011-08-10 10:45:20
如下代码,可以获取属性,方法,
printReltype 是一个控件,在 printReltype 中有一个成员 bindingSource(public System.Windows.Forms.BindingSource bindingSource)
我想调用 bindingSource 的Add方法,请教大家我如何获取 bindingSource ?


Assembly AppPrintTicket = Assembly.LoadFile(CommandLibrary.CommandClass.AppStartPath + "\\Tx.PrintTemplate.dll");
Type printReltype = AppPrintTicket.GetType("Tx.PrintTemplate.Epson.Default.TicketPrint", false, true);

Object printRelObj = Activator.CreateInstance(printReltype);
foreach (PropertyInfo _tmpProp in modelReltype.GetProperties()) //获取属性
{

}
...全文
104 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaojinhellonet 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dazhabai 的回复:]

propertyinfo 没有invoke方法,只有methodinfo之类的才有
[/Quote]
我不是要获取属性
我姓区不姓区 2011-08-10
  • 打赏
  • 举报
回复

Assembly AppPrintTicket = Assembly.LoadFile(CommandLibrary.CommandClass.AppStartPath + "\\Tx.PrintTemplate.dll");
Type printReltype = AppPrintTicket.GetType("Tx.PrintTemplate.Epson.Default.TicketPrint", false, true);

Object printRelObj = Activator.CreateInstance(printReltype);
FieldInfo fi = printReltype.GetField("bindingSource", BindingFlags.Public | BindingFlags.Instance);
if (fi != null)
{
Type bsType = fi.FieldType;
BindingSource bs = fi.GetValue(printRelObj) as BindingSource;
bs.Add(XXX);
}

dazhabai 2011-08-10
  • 打赏
  • 举报
回复
propertyinfo 没有invoke方法,只有methodinfo之类的才有
dazhabai 2011-08-10
  • 打赏
  • 举报
回复

public void InvokeDoubleBuffered(Control printReltype)
{
System.Reflection.PropertyInfo propertyInfo = printReltype.GetType().GetProperty("bindingSource", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
propertyInfo.SetValue(this, 你想赋的值, null);
}
catchdream 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 gaojinhellonet 的回复:]
如下代码,可以获取属性,方法,
printReltype 是一个控件,在 printReltype 中有一个成员 bindingSource(public System.Windows.Forms.BindingSource bindingSource)
我想调用 bindingSource 的Add方法,请教大家我如何获取 bindingSource ?

C# code

Assem……
[/Quote]
foreach (PropertyInfo _tmpProp in modelReltype.GetProperties()) //获取属性
{
if(_tmpProp is System.Windows.Forms.BindingSource)
//do your job
}
gaojinhellonet 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 gaojinhellonet 的回复:]
如下代码,可以获取属性,方法,
printReltype 是一个控件,在 printReltype 中有一个成员 bindingSource(public System.Windows.Forms.BindingSource bindingSource)
我想调用 bindingSource 的Add方法,请教大家我如何获取 bindingSource ?

C# code

Assem……
[/Quote]
呵呵,不是属性,是一个成员,我在构造函数里
this.bindingSource = new System.Windows.Forms.BindingSource(this.components);

其中bindingSource有Add方法(bindingSource.Add(Object obj)
我现在是要反射出 bindingSource 成员 调用 Add方法


ruanwei1987 2011-08-10
  • 打赏
  • 举报
回复
MSDN 上的例子,还供参考

[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
AllowMultiple = true) // multiuse attribute
]
public class Author : System.Attribute
{
string name;
public double version;

public Author(string name)
{
this.name = name;
version = 1.0; // Default value
}

public string GetName()
{
return name;
}
}

[Author("H. Ackerman")]
private class FirstClass
{
// ...
}

// No Author attribute
private class SecondClass
{
// ...
}

[Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
private class ThirdClass
{
// ...
}

class TestAuthorAttribute
{
static void Main()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
}

private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine("Author information for {0}", t);
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // reflection

foreach (System.Attribute attr in attrs)
{
if (attr is Author)
{
Author a = (Author)attr;
System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version);
}
}
}
}



输出

Author information for FirstClass

H. Ackerman, version 1.00

Author information for SecondClass

Author information for ThirdClass

H. Ackerman, version 1.00

M. Knott, version 2.00

我姓区不姓区 2011-08-10
  • 打赏
  • 举报
回复
先确定你的bindingSource 是属性还是字段
如果你是这么写的:
public System.Windows.Forms.BindingSource bindingSource
那它是字段
如果是
public System.Windows.Forms.BindingSource bindingSource
{
get;set;
}
这才是属性
  • 打赏
  • 举报
回复
看一下是字段还是属性
printReltype.GetField反射字段

Oj都已经写出来了
ly745455 2011-08-10
  • 打赏
  • 举报
回复
只能看懂理论。看不懂代码 ,真奇怪 !!
shaofei830927 2011-08-10
  • 打赏
  • 举报
回复
先用反射取属性值
在用反射取这个属性对象的Add方法

如果你有这个属性的类型 可以省略第二步

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧