XLua notes (2)

 XLua notes

1. XLua's enumeration conversion:

Previous projects have used XLua's cast to cast, can enumerations be cast? Tried it out today and found it doesn't work. After flipping through some blogs and adding my own practice, I sorted out five ways to use and convert enumerations:

C# creates a new test enumeration:

public enum TestEnum
{
    Null = 0,
}

 The Lua code test calls the C# enumeration:

Test(CS.TestEnum.Null)					--直接访问对应枚举
Test(CS.TestEnum.__CastFrom(0))			--调用__CastFrom方法,参数传枚举对应的值
Test(CS.TestEnum.__CastFrom("Null"))	--调用__CastFrom方法,参数传枚举对应的字符串
Test(0)									--直接使用枚举的值
Test("Null")							--直接使用枚举的字符串

2. Obtain the corresponding string of the enumeration:

The enumeration in C# can directly obtain the string corresponding to the enumeration with ToString(), but it cannot be obtained in Lua.

local _sid =  0
local _test = CS.TestEnum.__CastFrom(_sid)	--_test结果是枚举table(Null),打印出来是 ->  Null
local _result = tostring(_test)				--打印出来是 -> Null: 0

Neither _test nor _result is the result we want.

We can get the string corresponding to the enumeration through the System.Enum.GetName method:

local _result = CS.System.Enum.GetName(typeof(CS.TestEnum),sid)  -- _result结果是一个字符串:Null 

3. Get the value corresponding to the enumeration:

Similarly, the corresponding enumeration value can be obtained by calling the GetHashCode method of the enumeration.

local _result = CS.TestEnum.Null:GetHashCode() 

Guess you like

Origin blog.csdn.net/qq_33461689/article/details/121923929