| |

VerySource

 Forgot password?
 Register
Search
View: 968|Reply: 10

About dynamically taking the value of a variable name

[Copy link]

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-17 02:00:01
| Show all posts |Read mode
I have variable names:
private string AA = "aa";
private string BB = "bb";

public string this [string test]
{
  get
   {
code
}
}
Requirement, if the value of my test is AA (the requirement here also matches the variable name defined above), return: aa
If it is BB then return bb
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-20 12:15:01
| Show all posts
class Class1
{
static void Main (string [] args)
{
MyList list = new MyList ();
Console.WriteLine (list ["AA"]);
Console.WriteLine (list ["BB"]);
}
}

public class MyList
{
System.Collections.Hashtable hash = new System.Collections.Hashtable ();
public MyList ()
{
hash.Add ("AA", "aa");
hash.Add ("BB", "bb");
hash.Add ("CC", "cc");
}
public string this [string str]
{
get {return str + ":" + hash [str] .ToString ();}
}

}
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-21 10:45:02
| Show all posts
Thank you upstairs, I used WEB FORM here
It's in the MODEL, so I don't want to use HASHTABLE
See if there is a good solution
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-21 19:30:01
| Show all posts
I got it myself, thank you,

    private string HeiHei = "Look at it";

    public string HeiH
    {
        get
        {
            return HeiHei;
        }
    }
    public string this [string ss]
    {
        get
        {
          return this.GetType (). GetField (ss, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public) .GetValue (this) .ToString ();
        }
    }
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-21 21:15:01
| Show all posts
I do n’t know if this will affect the efficiency. Here, reflection is used. If you use the attribute directly, what will be the speed of the two
    public string HeiH
    {
        get
        {
            return HeiHei;
        }
    }
To complete the external value, let's discuss it here
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-21 23:00:01
| Show all posts
With reflection, performance will definitely drop a lot, why do n’t you want to use Hashtable?
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-22 10:15:01
| Show all posts
How about this?
public string this [string ss]
    {
        get
        {
            switch (ss) {
            case "AA":
                return "aa";
            case "BB":
                return "bb";
            ...
            }
        }
    }
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-4-22 16:30:01
| Show all posts
TO: I don't know if this will affect the efficiency. Here, reflection is used. If you use the attribute directly, what will be the speed of the two?

The reflection method is good, of course, the efficiency will be low ..

However, reflection is also more flexible. If the efficiency requirement is not very high, it is recommended to use reflection.
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-25 18:00:02
| Show all posts
Thank you, I'm posting about this too
Reply

Use magic Report

1

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Vietnam

 Author| Post time: 2020-4-25 23:45:01
| Show all posts
Using HASHTABLE in the MODEINFO layer seems a bit unworkable
 public class CartItemInfo {

        // Internal member variables
        private int quantity = 1;
        private string itemId;
        private string name;
        private string type;
        private decimal price;
        private string categoryId;
        private string productId;

        /// <summary>
        /// Default constructor
        /// </ summary>
        public CartItemInfo () {}

        /// <summary>
        /// Constructor with specified initial values
        /// </ summary>
        /// <param name = "itemId"> Id of item to add to cart </ param> </ param>
        /// <param name = "name"> Name of item </ param>
        /// <param name = "qty"> Quantity to purchase </ param>
        /// <param name = "price"> Price of item </ param>
        /// <param name = "type"> Item type </ param>
        /// <param name = "categoryId"> Parent category id </ param>
        /// <param name = "productId"> Parent product id </ param>
        public CartItemInfo (string itemId, string name, int qty, decimal price, string type, string categoryId, string productId) {
            this.itemId = itemId;
            this.name = name;
            this.quantity = qty;
            this.price = price;
            this.type = type;
            this.categoryId = categoryId;
            this.productId = productId;
        }

        // Properties
        public int Quantity {
            get {return quantity;}
            set {quantity = value;}
        }

        public decimal Subtotal {
            get {return (decimal) (this.quantity * this.price);}
        }

        public string ItemId {
            get {return itemId;}
        }

        public string Name {
            get {return name;}
        }

        public string Type {
            get {
                return type;
            }
        }
        public decimal Price {
            get {return price;}
        }

        public string CategoryId {
            get {
                return categoryId;
            }
        }
        public string ProductId {
            get {
                return productId;
            }
        }
    }
}

I want to change the attribute here to the index value, so I do n’t know how fast and efficient it will be. At the same time, if I use an indexer, do I have to go to the NEW one in advance?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list