Skip to main content
 首页 » 编程设计

c#之如何设置类属性的隐式分配

2024年02月13日14hnrainll

我有一个自定义类,想隐式地为其属性之一赋值。我知道 Microsoft 曾经将此内置到某些控件中,例如 TextBox1 = "Sets the TextBox1.Text property"。是因为它是编译器内置的还是可用的,所以只限于 Microsoft?

这看起来类似于隐式转换,但我需要一个结果实例来修改。

我的实际代码示例:

public class CustomObject<TObject> 
{ 
    public TObject BaseObject { get; set; } 
 
    //Psuedocode for what I want, this doesn't compile for multiple reason 
    public implicit operator CustomObject<TObject>(TObject FillIn) 
    { 
         this.BaseObject = FillIn; 
    } 
} 
 
//Usage 
var x = new CustomObject<int>(); 
x = 3; //this is the end result I want to code. 

请您参考如下方法:

C# 中的隐式重载运算符不能设置对象的属性,它只能转换自身为对象。

VB6 的默认属性在 C# 或 VB.NET 中不可用.