question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Square brackets added to nullable string

See original GitHub issue

I’m experiencing problems after declaring some strings as nullable, using the new nullable ref types in C# 8.

For any C# string?, the property.Type.Name will be string[].

Any idea how to get around this?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

2reactions
johnknoopcommented, Jul 17, 2020

@grey280 I got it working:

		string ProduceDictionaryType(Type type) {
			bool endsWithBrackets = type.TypeArguments.Last().Name.ToLower().EndsWith("[]");
			bool isEnumerable = type.TypeArguments.Last().FullName.StartsWith("System.Collections");
			bool isNullableString = endsWithBrackets && !isEnumerable;
			
			var suffix = isNullableString ? " | null | undefined" : "";
			
			var valueType = isNullableString ?
				$"{type.TypeArguments.Last().Name.Substring(0, type.TypeArguments.Last().Name.Length - 2)}"
				: type.TypeArguments.Last().Name;
			
			return $"{{ [key: {type.TypeArguments.First()}]: {valueType}{suffix}; }}";
		}
1reaction
SamuelKupferschmidcommented, Jun 24, 2020

For me this workaroud works:

if(p.Type.Name.EndsWith("[]") && !p.Type.FullName.StartsWith("System.Collections")) {
        return p.Type.Name.Substring(0, p.Type.Name.Length - 2);
 } else {
        return p.Type.Name;
 }

It would be nice if this project comes alive somehow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do the square brackets null the variable?
This make $xyz as an empty array. if you will vardump $xyz, you will see that it is an array type without having...
Read more >
How to skip null square brackets : r/Splunk
I wrote a regex to extract the values inside the square brackets. The regex I wrote works fine when both square brackets hold...
Read more >
Member access operators and expressions
Indexer operator [] Square brackets, [] , are typically used for array, indexer, or pointer element access.
Read more >
Using sed to remove both an opening and closing square ...
I know square brackets have special meaning in a regex so I'm escaping them by prepending with a backslash. The result I was...
Read more >
Python Empty List Tutorial – How to Create an ...
We typically use list() to create lists from existing iterables such as strings, dictionaries, or tuples. You will commonly see square brackets ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found