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

  • SEARCH
  • Home
  • 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 7039795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:28+00:00 2026-05-28T01:50:28+00:00

I’ve got a class defined like this: public abstract class Uniform<T> { public abstract

  • 0

I’ve got a class defined like this:

public abstract class Uniform<T>
{
    public abstract string GlslType { get; }
    ...
}

And then a subclass defined like this:

public class UniformInt : Uniform<int>
{
    public override string GlslType
    {
        get { return "int"; }
    }
}

And then a method somewhere else that looks like this:

    public static string GetCode<T>()
    {
        var sb = new StringBuilder();
        var type = typeof(T);
        sb.AppendFormat("struct {0} {{\n", type.Name);
        var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
        foreach(var f in fields)
        {
            sb.AppendFormat("    {0} {1};\n", f.FieldType.GetProperty("GlslType").GetValue(???), f.Name);
        }
        ...
    }

I’m having trouble filling in the ???s. I believe GetValue expects an instance of the object, but I don’t really care what instance it is because they all return the same value. And AFAIK there’s no such thing as a public abstract static readonly value, so I have to use properties.

So what can I put in place of those ???s to get back “int” (assuming one the fields was a UniformInt).

As a side: How can I limit fields to only field types that inherit Uniform<>?

  • 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-28T01:50:29+00:00Added an answer on May 28, 2026 at 1:50 am

    The problem is that since your property is not static the compiler doesn’t know that they all return the same value. Since your UniformInt is not sealed, another user could inherit from it and override GlslType to return something else. Then UniformInt and all derived classes could be used for your GetCode<T>() method.

    A static method would really be the best option. To make sure that you implement them on all classes (something you can’t force because static methods can’t be abstract) I would write a simple unit test that uses reflection to load all classes that inherit from Uniform<T> and check if they have the static property defined.

    UPDATE

    When thinking about how Attributes could help and after some experimenting I came up with the following. It definitely won’t win a beauty contest but as a learning exercise it was helpful 😉

    using System;
    using System.Linq;
    
    namespace StackOverflow
    {
        internal class StackOverflowTest
        {
            private static void Main()
            {
                string sInt = UniformInt.GlslType;
                string sDouble = UniformDouble.GlslType;
            }
        }
    
        public abstract class Uniform<B, T> // Curiously recurring template pattern 
            where B : Uniform<B, T>
        {
            public static string GlslType
            {
                get
                {
                    var attribute = typeof(B).GetCustomAttributes(typeof(GlslTypeAttribute), true);
    
                    if (!attribute.Any())
                    {
                        throw new InvalidOperationException(
                            "The GslType cannot be determined. Make sure the GslTypeAttribute is added to all derived classes.");
                    }
    
                    return ((GlslTypeAttribute)attribute[0]).GlslType;
                }
            }
        }
    
        [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
        internal sealed class GlslTypeAttribute : Attribute
        {
            public string GlslType { get; private set; }
    
            public GlslTypeAttribute(string glslType)
            {
                GlslType = glslType;
            }
        }
    
        [GlslType("int")]
        public class UniformInt : Uniform<UniformInt, int> // Curiously recurring template pattern 
        {
        }
    
        [GlslType("double")]
        public class UniformDouble : Uniform<UniformDouble, double> // Curiously recurring template pattern 
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but

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.