2

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.

enter image description here

1 Answer 1

6

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Got it, typeof(SqlBytes).MakeByRefType()
@RemusRusanu yup; I've edited to show both by-val <===> by-ref operations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.