簡體   English   中英

能將賦值作為參數傳遞的函數定義是什么

[英]What is the function definition to be able to pass an assignment as a parameter

我正在重構一些舊代碼。 現在我面臨的問題之一是

 AllFields.Add(SomeProperty = new IntField
            {
                ColumnName = "SomeProperty",
                DisplayName = "Some Property",
                IsEditable = false,
                IsRequired = true
            });

現在我想要的是某種擴展方法,該方法將SomeProperty = new IntField()的賦值作為參數

例如

public static class Extensions  
{  
  public static void Add(this IList<IField> source, SomeParameter value)  
  {  
  }  
}  

這樣我就可以

IList<IField> AllFields = new List<IField>();  
AllFields.Add(SomeProperty = new IntField(){});
AllFields.Add(SomeProperty2 = new DecimalField(){});

我忘了補充。 SomeProperty和SomeProperty的類型為int和十進制

您可以為此使用泛型。 如下所示就足夠了:

public static class Extensions  
{  
    public static void Add<T>(this IList<T> source, SomeParameter value) 
        where T : IField, new()  
    {  
    }  
} 

本質上,您定義一個泛型方法,該類的唯一泛型參數T應該具有無參數構造函數( where T : new() )並實現接口IField

這樣做,您可以編寫類似以下內容的內容:

IList<IField> AllFields = new List<IField>();  
AllFields.Add(new IntField(){});
AllFields.Add(new DecimalField(){});

您還可能會遇到類似以下內容的信息:

IField SomeProperty;
IField SomeProperty2;

IList<IField> AllFields = new List<IField>();  
AllFields.Add(SomeProperty = new IntField(){});
AllFields.Add(SomeProperty2 = new DecimalField(){});

但是,從您描述過要執行的操作中,我看不到有任何用法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM