本文介绍了Vb.net如何在没有先前实例的情况下收到扩展方法的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我不知道如何用文字说明这一点,以颜色为例: 我知道怎么写基本< Extension()> 方法: < Extension()> 公共 功能 GetRedValue( byval c as Color) as byte 返回 cR 结束 功能 我可以使用: Dim Col as Color = Color.LimeGreen Label1.Text = Col.GetRedValue()。ToString 它会按预期返回 Color.R 的值。 我想弄清楚的是如何在没有创建颜色实例的情况下运行方法以返回该对象的新实例。 看起来Color结构可以固有地做到这一点。当您创建新颜色时,您可以使用预定义颜色或定义对象,如: Dim col as Color = Color.FromARGB(Alpha,Red,Green,Blue) 没有创建实例。 如何创建自己的方法以同样的方式工作 示例: <扩展()> 公共 功能 FromHSV( byval c as 颜色,Hue as 单 ,饱和度 as Single ,Value as Single ) as 颜色 Dim ReturnColor as Color = ConvertToRGB(Hue,Saturation,Value)' << ==我自己的例程返回颜色 返回 ReturnColor 结束 功能 所以我可以使用它像: Dim Col as Color = Color.FromHSV( 0 。 5 , 0 。 2 , 1 . 0 ) 甚至 Dim Col as Color = Color.MyCustomColor 我希望自己能说清楚,因为我不确定我要完成的正确术语。 感谢所有帮助,谢谢! 我尝试过的事情: 我认为共享参数可行,但你不能在模块中使用 Shared 你不能在课堂上使用< Extension()> 。 我搜索过没有找到任何帮助。解决方案 扩展方法(视觉基本) [ ^ ] :扩展方法可以编写一个可以被调用的方法,就好像它是现有类型的实例方法。 扩展方法只能用作类型的实例方法。你试图在类型上定义一个新的共享方法,这是不可能的。 相反,您需要使用模块名称来调用您的函数: 公共 模块 ColorExtensions 公共 功能 FromHSV( ByVal hue 作为 单, ByVal 饱和度作为 单, ByVal 值作为 单个)作为颜色 ... 结束 功能 结束 模块 ... Dim col As Color = ColorExtensions.FromHSV( 0 。 5 , 0 。 2 , 1 。 0 ) I don't know how exactly to put this in words, Using a color as an example:I know how to write basic <Extension()> methods:<Extension()>Public Function GetRedValue(byval c as Color) as byte Return c.REnd FunctionI could use:Dim Col as Color = Color.LimeGreenLabel1.Text = Col.GetRedValue().ToStringAnd it would return the value of Color.R as expected.What I am trying to figure out is how to run a method without having an instance of color already created in order to return a new instance of that object.It seems like the Color structure can inherently do it. When you create a new color you can use a predefined color or define the object like:Dim col as Color = Color.FromARGB(Alpha, Red, Green, Blue)without having an instance already created.How to I create my own method that work in the same fashionExample:<Extension()>Public Function FromHSV(byval c as Color, Hue as Single, Saturation as Single, Value as Single) as Color Dim ReturnColor as Color = ConvertToRGB(Hue, Saturation, Value) '<<== My Own Routine to return a colorReturn ReturnColorEnd FunctionSo that I may use it like:Dim Col as Color = Color.FromHSV(0.5, 0.2, 1.0)or evenDim Col as Color = Color.MyCustomColorI'm hoping I am making myself clear as I was not sure of the correct terminology for what I am trying to accomplish.Any help is appreciated, Thank You!What I have tried:I thought the Shared parameter would work, but you can't use Shared within a module and you can't use <Extension()> within a class.I have searched and found nothing that helps. 解决方案 Extension Methods (Visual Basic)[^]:Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.Extension methods can only be used as instance methods on the type. You're trying to define a new Shared method on the type, which isn't possible.Instead, you'll need to use your module name to call your function:Public Module ColorExtensions Public Function FromHSV(ByVal hue As Single, ByVal saturation As Single, ByVal value As Single) As Color ... End FunctionEnd Module...Dim col As Color = ColorExtensions.FromHSV(0.5, 0.2, 1.0) 这篇关于Vb.net如何在没有先前实例的情况下收到扩展方法的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!