Home > Backend Development > C++ > How Can I Dynamically Retrieve Attribute Values from a Class at Runtime?

How Can I Dynamically Retrieve Attribute Values from a Class at Runtime?

Linda Hamilton
Release: 2025-01-12 06:04:12
Original
895 people have browsed it

How Can I Dynamically Retrieve Attribute Values from a Class at Runtime?

Retrieve properties at runtime

This article introduces a general method for dynamically accessing and extracting attribute values ​​​​of a class.

Use dedicated methods

Define a generic method that accepts type parameters:

<code class="language-csharp">public string GetDomainName<T>()</code>
Copy after login

Internal method:

  • Use typeof(T).GetCustomAttributes to retrieve custom properties:

    <code class="language-csharp">  var dnAttribute = typeof(T).GetCustomAttributes(
        typeof(DomainNameAttribute), true
      ).FirstOrDefault() as DomainNameAttribute;</code>
    Copy after login
  • If the attribute exists, return its value:

    <code class="language-csharp">  if (dnAttribute != null)
      {
        return dnAttribute.Name;
      }</code>
    Copy after login
  • Otherwise, return null:

    <code class="language-csharp">  return null;</code>
    Copy after login

Utility extension methods

For wider applicability, generalize this method to handle any attribute:

<code class="language-csharp">public static class AttributeExtensions
{
    public static TValue GetAttributeValue<TAttribute, TValue>(
        this Type type, 
        Func<TAttribute, TValue> valueSelector) 
        where TAttribute : Attribute
}</code>
Copy after login

Internal extension method:

  • Retrieve custom attributes:

    <code class="language-csharp">  var att = type.GetCustomAttributes(
        typeof(TAttribute), true
      ).FirstOrDefault() as TAttribute;</code>
    Copy after login
  • If the attribute exists, use the provided valueSelector to extract the required value:

    <code class="language-csharp">  if (att != null)
      {
        return valueSelector(att);
      }</code>
    Copy after login
  • Otherwise, return the default value of the type:

    <code class="language-csharp">  return default(TValue);</code>
    Copy after login

Usage examples

  • Retrieve the MyClass attribute of DomainName:
<code class="language-csharp">string name = typeof(MyClass).GetDomainName<MyClass>();</code>
Copy after login
  • Retrieve any attribute value of MyClass using extension methods:
<code class="language-csharp">string name = typeof(MyClass)
    .GetAttributeValue<DomainNameAttribute, string>((DomainNameAttribute dna) => dna.Name);</code>
Copy after login

The above is the detailed content of How Can I Dynamically Retrieve Attribute Values from a Class at Runtime?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template