Site Logo
Looking for girlfriend > Looking for a husband > C# get friendly type name

C# get friendly type name

An Enumeration or enum is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language. Sometimes you might need to loop on the elements of a particular enum and print its element names, however as you know by the language constraints, the enum element names must follow the naming convention, you cannot include spaces or other special characters. Therefore printing the elements will lead to printing the defined non-friendly elements names. In this article, I will be explaining and showing to you by a code sample how you can define and display friendly names for enumeration elements using the Description annotation.

Content:
SEE VIDEO BY TOPIC: Implementing SSL in asp net web application Part 101

Subscribe to RSS

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I need some way to get the Name of a Type, when type. Namespaces, standard aliases for built-in types , and use of StringBuilder left as an exercise for the reader.

My take on yoyo's approach. Ensures more friendly names for primitives, handles arrays and is recursive to handle nested generics. Also unit tests. Here is my take on this. I did not put the backtick check since for what I see, it's always there. You can add it if you want but I like to keep things simple. The optimal solution appeared to be Ali's solution, but it doesn't work for nested types:. I spent some time trying to resolve the nested types issue so wanted to document this here to ensure anyone else in future can save some considerable time and headaches!

I have checked the performance as well, and it is in the microseconds to complete 8 microseconds in the case of the last scenario:.

Here is a complete implementation based on the previous answers supporting both Aliases including Nullable and Arrays:. Learn more. Asked 9 years, 6 months ago. Active 3 months ago.

Viewed 41k times. James Kovacs Entity Entity 6, 18 18 gold badges 65 65 silver badges bronze badges. Active Oldest Votes. Show g. Remove g. LukeH LukeH k 48 48 gold badges silver badges bronze badges. GetGenericArguments [0]. I needed this a while ago and couldn't find it anywhere. If you remove everything after the backtick you loose any array brackets that might be present.

Name; if type. Show t. I know the OP didn't ask for the generic type parameters, but I prefer it that way.

Join ",", type. Remove iBacktick ;. You need to use type. IsArray to test for this and type. GetArrayRank to determine how many dimensions the array had. AreEqual "string", typeof string. FriendlyName ; Assert. AreEqual "int[]", typeof int[]. AreEqual "int[][]", typeof int[][]. Humberto 6, 2 2 gold badges 27 27 silver badges 45 45 bronze badges. Nick Nick 7 7 silver badges 11 11 bronze badges. Does this compile? I had to tweak a bit. Edited for corrections.

You forgot about Nullable. To prettify nullables you should use something like this: if type. This has the same problem as yoyo's answer in that it doesn't check for arrays and strips away any [] brackets from the name.

The aliasing doesn't handle array's either; Use type. IsArray and type. GetElementType to resolve aliases for arrays. I updated my answer about type aliases to show how one can do that. Alex Wiese 7, 4 4 gold badges 36 36 silver badges 65 65 bronze badges. Johannes Rudolph Johannes Rudolph Name; if!

Ali Ali 9 9 silver badges 11 11 bronze badges. Substring 0, type. Yepeekai Yepeekai 1, 18 18 silver badges 17 17 bronze badges. FullName; if type. Append t. Substring 0, iAssyBackTick - 1. Replace "[", string.

Add genType. GetTypeString genType : genType. Int32 System. I have checked the performance as well, and it is in the microseconds to complete 8 microseconds in the case of the last scenario: Performance results Variables names used from original scenario list "i" 43uS "listTest" 3uS "dictTest" 2uS "nestTest" 5uS "superNestTest" 9uS "superNestTest2" 9uS Average times after performing the above code times on each scenario.

Chris Watts Chris Watts 1 1 gold badge 4 4 silver badges 20 20 bronze badges. Thomas Haller Thomas Haller 1 1 silver badge 11 11 bronze badges. Remove iBacktick : type. Join includeSpaceAfterComma? GetTypeCode type ]! GetFriendlyName ; Console.

AnorZaken AnorZaken 1, 1 1 gold badge 15 15 silver badges 24 24 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast An emotional week, and the way forward. The Overflow The way forward. Featured on Meta. Threshold experiment results: closing, editing and reopening all become more….

It's time to reward the duplicate finders. Linked 1. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.

C# stuff nobody told you – enum is a b*tch!

Generic; using System. IO; using System. Linq; using System. Reflection; using System.

Here: In this example, an int is provided with the value 3 and the correct MedicationType is returned. Tip: The typeof operator invokes reflection and the Enum. GetNames method also internally accesses the metadata representation in memory.

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I need some way to get the Name of a Type, when type. Namespaces, standard aliases for built-in types , and use of StringBuilder left as an exercise for the reader. My take on yoyo's approach.

C# Enum.GetName, GetNames: Get String Array From Enum

Visual Studio updates for this book are now available. Follow the Download Source Code link for this book on the Apress website. It contains detailed explanations of the new Core MVC functionality which enables developers to produce leaner, cloud optimized and mobile-ready applications for the. NET platform. This book puts ASP. NET Core MVC into context and dives deep into the tools and techniques required to build modern, cloud optimized extensible web applications. All the new MVC features are described in detail and the author explains how best to apply them to both new and existing projects. Best-selling author Adam Freeman has thoroughly revised this market-leading book and explains how to get the most from ASP. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need.

Using .NET Generics with a type derived at runtime

This overview page is a summary contains a shorter version of all the release notes for TypeScript. Recent versions of TypeScript around 3. Unfortunately, that introduced a few regressions, especially when mixing in values with null or undefined. This is strange behavior!

C tuples can have optional element names. Here are some interesting details about tuple element names and how they are treated by the language.

For the first article, I will show you how friendly looking, always helpful, good guy enum can become an asshole. An asshole who will steal your precious time! The situation was strange… We have a method that takes our enum as a parameter and processes it somehow.

Get Friendly Type Name : Type « Reflection « C# / C Sharp

Represents a collection of Oid objects. This class cannot be inherited. The following code example shows how to use the OidCollection class.

Visual Studio updates for this book are now available. Follow the Download Source Code link for this book on the Apress website. It contains detailed explanations of the new Core MVC functionality which enables developers to produce leaner, cloud optimized and mobile-ready applications for the. NET platform. This book puts ASP.

C# Tuples. More about element names.

I have used. NET Generic types a lot and found them really useful. They form a core part of the solution in my open-source GenericServices library and my private GenericActions library. This article explores why and how I use Generics and some of the techniques I use to hide the more complex type definitions. Even if you are not interested in my particular use of Generics this article has some useful information on:. If you are not familiar with Generics then look at this introduction. The primary thing about Generics is it allows you to write one code implementation that can then work on a range of types. The down side of Generics is you normally have to know the type at compile time.

Get strings and string arrays from an enum. goodworksqatar.come. An enum type has names. These names are string representations of the enum values. The.

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Is there an easy way without writing a recursive method which will give a 'user friendly' name for a generic type from the Type class? You can avoid writing a recursive method by calling the recursive method that's already provided for you:. Note that this includes the type namespaces, but excludes the assembly references.

Create the Friendly Name of Any Type

.

C# Tuples. More about element names.

.

.

.

.

.

Comments: 2
  1. Mazusar

    I apologise, but, in my opinion, you are not right. I am assured. I can prove it.

  2. Goltit

    In it something is. Now all became clear, many thanks for the help in this question.

Thanks! Your comment will appear after verification.
Add a comment

© 2020 Online - Advisor on specific issues.