简体   繁体   中英

ParameterInfo.ParameterType is “SqlBytes&”, how to compare with typeof()

I'm writing an automated SQLCLR deployment tool and I'm using reflection to discover the procedures and functions that have to be declared. So I'm using code like this to build the T-SQL needed to deploy the assembly methods:

...
if (p.ParameterType == typeof(string))
{
   sql = "nvarchar(4000)";
}
...

But this method contains an parameter declared in C# as out SqlBytes bytes and the ParameterType is SqlBytes& . I cannot use typeof(SqlBytes&) for comparison, because is invalid syntax. So I'm a bit puzzled what exactly is the SqlBytes& type, and if there is a way to produce the typeof for it. I know I can resort to types name (ie. strings) comparison instead, that is not my the question, I'm more curious what is such a type with & , seems like a C++ reference type, but I reckon in +10 years of working with .Net I've never noticed them.

在此处输入图片说明

SqlBytes& is exactly the same as out SqlBytes (and ref SqlBytes , as it happens). Basically, out and ref are implemented identically, as a reference, by-reference. If you are using reflection and you currently have a Type instance, you can get the by-reference version by using:

Type type = ...
Type byRefType = type.MakeByRefType();

and rather confusingly, to get back to the non-by-ref version:

Type origType = byRefType.GetElementType();

(I suspect they probably just hacked that into an existing method, frankly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM