如何模拟对象中的缺失属性 [英] How to emulate missing property in object

查看:65
本文介绍了如何模拟对象中的缺失属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现实体对象,当首次引用属性时,它应该从

数据库填充其属性。


在RDL报告中我使用对象属性喜欢


MyObject.MyProperty


MyObject是MyEntity类的实例。在设计时

MyObject中没有MyProperty属性。


所以我需要捕获缺少的属性引用并在运行时提供它的值

时间。

行内容


公共类MyEntity {

覆盖异常MissingPropertyGetException {

return dbo.ExecuteScalar(" SELECT" + Exception.UnreferencedPropertyName +

" FROM myEntity");

}

}


知道怎么实现这个吗?


Andrus。

解决方案

< blockquote>如果RDL通过组件模型使用标准数据绑定(我希望它与DataTable的兼容性等等),那么或许看看

到ICustomTypeDescriptor(1.1以后)在TypeDescriptionProvider上

(2.0起);您可以提供自己的运行时属性,这些属性可以与您选择的类似物或物品相同(尽管反射是默认值)。
。 >

Marc


作为TypeDescriptionProvider的一个例子,或许可以看一下:

http://www.codeproject.com/csharp/Hy...Descriptor.asp


忘记所有的Reflection.Emit东西(这是偏离主题的方式);

重要的一点是覆盖

CustomTypeDescriptor .GetProperties();从base获取原始集合

,复制到列表中,添加新的虚拟属性并将列表推送到新的PropertyDescriptionCollection中。


Marc


重要的一位是CustomTypeDescriptor.GetProperties()的覆盖;


从base获取原始集合,复制到列表中,添加新的

虚拟属性并推送列表到一个新的

PropertyDescriptionCollection。



Marc,


感谢您提供优质信息。

我发现我需要模拟静态方法调用,因为FYIReporting引擎

不允许在报告中使用属性。


我是C#的新手,所以我在下面创建了测试用例你的文章。


如何捕获HyperTypeDescriptionProvider类中的MyStaticMethod调用和

返回字符串" test" ?

Andrus。


使用System.ComponentModel;


使用System;


使用System.Reflection;


静态类程序{


static void Main(){


类型t = typeof(MyEntity);


MethodInfo mInfo = t.GetMethod(" MyStaticMethod");


对象returnVal = mInfo.Invoke(t,null);


System.Windows.Forms.MessageBox.Show(returnVal.ToS tring());

}


}


[TypeDescriptionProvider(typeof(HyperTypeDescriptio nProvider))]


class MyEntity {


//我需要模拟以下方法:


//公共静态字符串MyStaticMethod(){return "测试英寸; } $ / $

密封类HyperTypeDescriptionProvider:TypeDescriptionProvider {


//如何捕获MyStaticMethod call和return stringtest ?


}


I''m implementing entity object which should populate its properties from
database when property is first referenced.

In RDL reports I use object properties like

MyObject.MyProperty

MyObject is instance of MyEntity class. There is no MyProperty property in
MyObject at design time.

So I need to catch missing property reference and provide its value at run
time.
Something line

public class MyEntity {
override exception MissingPropertyGetException {
return dbo.ExecuteScalar("SELECT "+Exception.UnreferencedPropertyName+
" FROM myEntity");
}
}

Any idea how to implement this ?

Andrus.

解决方案

If RDL uses standard data-binding via the component model (which I
would expect for compatibility with DataTable etc), then perhaps look
into ICustomTypeDescriptor (1.1 onwards) on TypeDescriptionProvider
(2.0 onwards); you can provide your own runtime properties that can
bear as little or as much resemblance as you choose to the class
properties (although reflection is the default).

Marc


As an example of TypeDescriptionProvider, perhaps look here:

http://www.codeproject.com/csharp/Hy...Descriptor.asp

Forget about all the Reflection.Emit stuff (that is way off topic);
the important bit is the override of
CustomTypeDescriptor.GetProperties(); get hold of the original set
from "base", copy into a list, add your new dummy properties and push
the list into a new PropertyDescriptionCollection.

Marc


the important bit is the override of CustomTypeDescriptor.GetProperties();

get hold of the original set from "base", copy into a list, add your new
dummy properties and push the list into a new
PropertyDescriptionCollection.

Marc,

thank you for excellent information.
I found that I need to emulate static method call since FYIReporting engine
does not allow to use properties in reports.

I''m new to C# so I created testcase below based on your article.

How to catch MyStaticMethod call in HyperTypeDescriptionProvider class and
return string "test" ?
Andrus.

using System.ComponentModel;

using System;

using System.Reflection;

static class Program {

static void Main() {

Type t = typeof(MyEntity);

MethodInfo mInfo = t.GetMethod("MyStaticMethod");

Object returnVal = mInfo.Invoke(t, null);

System.Windows.Forms.MessageBox.Show(returnVal.ToS tring());

}

}

[TypeDescriptionProvider(typeof(HyperTypeDescriptio nProvider))]

class MyEntity {

// I need to emulate the following method:

// public static string MyStaticMethod() { return "test"; }

}

sealed class HyperTypeDescriptionProvider : TypeDescriptionProvider {

// How to catch MyStaticMethod call and return string "test" ?

}


这篇关于如何模拟对象中的缺失属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆