本文介绍了我可以有一个通用的此[]属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一组对象从DataElement所有派生的DICOM字典。
本字典有一个int作为重点,而DataElement财产。
我的DICOM字典包含此[]属性在那里我可以访问DataElement,像这样的:

I have a DICOM dictionary that contains a set of objects all deriving from DataElement.The dictionary has an int as a key, and the DataElement as property.My DICOM dictionary contains a this[] property where I can access the DataElement, like this:

public class DicomDictionary
{
  Dictionary<int, DataElement> myElements = new Dictionary<int, DataElement>();
  .
  .
  public DataElement this[int DataElementTag]
  {
    get
    {
      return myElements[int];
    }
  }
}

一个现在的问题是我有不同类型的DataElement全部来自DataElement派生,像DataElementSQ,DataElementOB等。我想现在要做的是以下作出书面形式在C#中稍微容易一些:

A problem now is that I have different DataElement types all deriving from DataElement, like DataElementSQ, DataElementOB and so on. What I wanted to do now is the following to make writing in C# a little bit easier:

 public T this<T>[int DataElementTag] where T : DataElement
 {
   get
   {
      return myElements[int];
   }
 }



但是,这是不是真的有可能。是不是我错过了什么?当然,我可以用getter方法做到这一点,但它会好得多有这样的看法。

But this is not really possible. Is there something I have missed? Of course I could do it with Getter method, but it would be much nicer to have it this way.

推荐答案

它是一个?情况下,你

public class DicomDictionary<TElement>
{
        Dictionary<int, TElement> myElements = new Dictionary<int, TElement>();
        public TElement this[int DataElementTag]
        {
              get
              {
                 return myElements[int];
              }
        }
}

这篇关于我可以有一个通用的此[]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 03:04