首页 \ 问答 \ 使用反射从元数据类中获取属性属性(Using reflection to get property attributes from a metadata class)

使用反射从元数据类中获取属性属性(Using reflection to get property attributes from a metadata class)

我有一个名为Project的LINQ生成类。

我想为生成的属性添加一些属性,因此我使用MetadataType属性定义了一个部分类。

[MetadataType(typeof(Project_HiddenProps))]
public partial class Project : IProject
{
    // There are also a few additional properties defined here.
}

public class Project_HiddenProps
{
    [HiddenColumn]
    public System.Guid Id { get; set; } 
    // Id is an auto-generated property that I've added a custom attribute to
}

后来,我使用反射来尝试获取此属性的属性。

var customAttributes = prop.GetCustomAttributes(false);

但是,我只获得了System.Data.Linq.Mapping.ColumnAttribute类型的一个属性。 我的HiddenColumn属性未包含在集合中。 我的问题是如何使用反射来获取这些元数据属性。

任何帮助将不胜感激。


I have a LINQ generated class called Project.

I wanted to add some attributes to the generated properties, so I defined a partial class with the MetadataType attribute.

[MetadataType(typeof(Project_HiddenProps))]
public partial class Project : IProject
{
    // There are also a few additional properties defined here.
}

public class Project_HiddenProps
{
    [HiddenColumn]
    public System.Guid Id { get; set; } 
    // Id is an auto-generated property that I've added a custom attribute to
}

Later, I use reflection to try to get the attributes of this property.

var customAttributes = prop.GetCustomAttributes(false);

I only get one attribute, though, of type System.Data.Linq.Mapping.ColumnAttribute. My HiddenColumn attribute is not included in the collection. My question is how to get at these metadata properties using reflection.

Any help would be greatly appreciated.


原文:https://stackoverflow.com/questions/7742046
更新时间:2024-02-21 10:02

最满意答案

好吧,那里你有很多泡菜。
一方面,您需要在表上使用instead of insert触发器,但另一方面,您希望获得此触发器生成的标识返回到激活它的存储过程。

由于无法向触发器发送参数,因此您必须执行以下三种操作之一:

  1. 找到一些方法来消除对它的需要而不是触发器。
    这是我最好的建议。

  2. 将存储过程分为两部分:一部分将执行所有操作,直到insert into语句(包括它),从而激活instead of insert触发器,以及另一部分将执行触发后所需的所有操作。 这样,您可以使用scope_identity() instead of insert触发器,并将其返回值作为参数发送到第二个存储过程。
    注意:此设计意味着您必须逐个插入记录。 如果您尝试插入多条记录, scope_identity()将仅返回触发器插入的最后一行的标识。

  3. 找到一些在存储过程和而不是触发器之间传递数据的方法。 由于触发器不能除了或返回参数,因此您必须使用临时表或常规表。 建议的解决方案仅作为最后的手段,因为它会使您的代码复杂化,并可能导致一些性能问题。 此外,您必须找到一种方法来执行存储过程,直到触发器完成它的工作。 我可以给你一些关于如何在程序和触发器之间共享数据的指针,但我真的建议不要选择这个解决方案。

Well, you've got yourself quite a pickle there.
From the one hand, you need the instead of insert trigger on your table, but from the other hand, you want to get the identity that this trigger generates back to the stored procedure that activated it.

Since there is no way to send parameters to and from triggers, you will have to do one of 3 things:

  1. Find some way to eliminate the need for that instead of trigger.
    This is my best recommendation.

  2. Break your stored procedure to 2 parts: One part will do everything until the insert into statement (including it), thus activating the instead of insert trigger, and the other part that will do all operations needed after the trigger. this way you can use the scope_identity() inside the instead of insert trigger and send it's return value to the second stored procedure as a parameter.
    Note: this design means you have to insert records one by one. should you try to insert more then one record, scope_identity() will only return the identity of the last row inserted by the trigger.

  3. Find some way of passing data between the stored procedure and the instead of trigger. Since triggers can't except or return parameters, you will have to use either a temporary table or a regular table. This suggested solution is only suggested as a last resort, since it will complicate your code and probably cause some performance issues as well. Also, you will have to find a way to hold execution of the stored procedure until the instead of trigger will finish it's work. I can give you some pointers on how to share data between the procedure and the trigger, but I really suggest not to choose this solution.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。