Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6131115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:56:12+00:00 2026-05-23T16:56:12+00:00

i am open your ideas: i really dislike this usage . how to convert

  • 0

i am open your ideas: i really dislike this usage . how to convert enum to string via attributes. But please be careful my question StringValueAttribute have more than one constructor also fields. How to develop GetStringValue Extention method via StringValueAttribute ??? Best Regards…



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace UsingAttributes
{
        static void Main(string[] args)
        {
            Console.WriteLine(UserType.Login.GetStringValue());
            Console.Read();
        }
    }
    public enum UserType : int
    {
        [StringValueAttribute("xyz", "test")]
        Login = 1
    }
    public class StringValueAttribute : Attribute
    {
        public string UserName { get; protected set; }
        public string PassWord { get; protected set; }
        public decimal Something { get; protected set; }

          public StringValueAttribute(string Username, string Password,decimal something)
        {
            UserName = Username;
            PassWord = Password;
            Something  =something;
        }
        public StringValueAttribute(string Username, string Password)
        {
            UserName = Username;
            PassWord = Password;
        }
       public StringValueAttribute(string Username)
        {
            UserName = Username;

        }

    }

    public static class Extentions
    {
        public static String[] GetStringValue(this Enum value)
        {
            // Get the type
            Type type = value.GetType();

            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());

            // Get the stringvalue attributes
            StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(StringValueAttribute), false) as StringValueAttribute[];

            // Return the first if there was a match.


                PropertyInfo[] pi = attribs[0].GetType().GetProperties();
                String[] Vals = new String[pi.Length];
                int i = 0;
                foreach (PropertyInfo item in pi)
                {
                   // Vals[i] =  How to return String[] 

                }
            // i dislike return values one by one : attribs[0].UserName 
           //return attribs.Length > 0 ? attribs[0].UserName : null; // i have more values
        }
    }
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:56:12+00:00Added an answer on May 23, 2026 at 4:56 pm

    Firstly note that decimal cannot be used as an attribute argument. You can use double or you can use a string and then convert it to a decimal.

    Your GetStringValue() should work if you replace

        // Vals[i] =  How to return String[]
    

    with

        Object val = item.GetValue(attribs[0], null);
        Vals[i++] = val != null ? val.ToString() : "";
    

    If you are using C# 4.0, you don’t need three constructors. You can use optional arguments:

    public StringValueAttribute(string Username, string Password = "nothing", double something = 0)         
    {             
         ...
    }         
    

    And in older versions of C# you can use the OptionalAttribute:

    public StringValueAttribute(string Username, [Optional] string Password, [Optional] double something)         
    {             
         ...
    }         
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Besides open-sourcing your project and legislation, are there ways to prevent, or at least
From your experience, what is the most accurate open-source Optical Character Recognition (OCR) library/software
In your applications, what's a long time to keep a transaction open before committing
What is the shortcut to open a file within your solution in Visual Studio
Open up a Rails console and enter this: 2.weeks.ago.between? 2.weeks.ago, 1.week.ago Did it give
I really like the idea of reading others people code to improve your design
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java.
I know this isn't strictly a programming question but y'all must have experienced this.
I've been at this all day long and honestly I'm out of ideas. I
I have this small code library that I'm considering releasing into Open Source. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.