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 9266577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:21:05+00:00 2026-06-18T14:21:05+00:00

I’m trying to code DRY, and I have the following setup, which Visual Studio’s

  • 0

I’m trying to code DRY, and I have the following setup, which Visual Studio’s Code Analysis system is telling me is unwise:

public abstract class ShaderBase
{

    protected ShaderBase(Device device, string vertexShaderString, string pixelShaderString)
    {
        ShaderSignature inputSignature;
        using (ShaderBytecode bytecode = ShaderBytecode.CompileFromFile(vertexShaderString, "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
        {
            vertexShader = new VertexShader(device, bytecode);
            inputSignature = ShaderSignature.GetInputSignature(bytecode);
        }

        inputLayout = MakeInputLayout(device,inputSignature);
    }

    protected abstract InputLayout MakeInputLayout(Device device, ShaderSignature inputSignature);
}

public class TextureShader:ShaderBase
{
    public ColourShader(Device device) : base(device,"shaders/colour.fx", "shaders/colour.fx")
    {
    }

    protected override InputLayout MakeInputLayout(Device device, SlimDX.D3DCompiler.ShaderSignature inputSignature)
    {
        return new InputLayout(device, inputSignature, new[] { 
            new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0), 
            new InputElement("COLOR",0,SlimDX.DXGI.Format.R32G32B32_Float,0)
        });
    }
}

So as you can see, I have a base class, from which I have multiple derived classes. Each derived class uses a different InputLayout and must therefore use a different implementation of MakeInputLayout, which is why I override it. However, every derived class must execute the code I’ve put in the base class’s constructor, including the call to whichever implementation of MakeInputLayout that derived class has.

I’m trying to avoid code duplication where possible, but Microsoft is suggesting I should never be calling overrideable functions in base class constructors, even though none of the overriden implementations ever depend on values set up at runtime (they could, technically, have been labelled static, if c# allowed us to override statics).

What I want to know is, what’s the acceptable way of having a base class force derived classes to call their own derived implementation of a function in their constructors? Or am I just going to have to copy and past some code around and reduce the maintainability of the system?

  • 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-06-18T14:21:06+00:00Added an answer on June 18, 2026 at 2:21 pm
    1. You can ignore Visual Studio’s Code Analysis, if you 100% sure that it will not be a problem in your case. However, it might be not very safety and personally I do not recomend to do so.

    2. Use helper interfaces/classes/delegates to avoid constructor virtual method call:

      public interface IInputLayoutMaker
      {
          InputLayout MakeInputLayout(Device device, SlimDX.D3DCompiler.ShaderSignature inputSignature);
      }
      
      public abstract class ShaderBase
      {
          protected ShaderBase(Device device, string vertexShaderString, string pixelShaderString, IInputLayoutMaker inputLayoutMaker)
          {
              ShaderSignature inputSignature;
              using (ShaderBytecode bytecode = ShaderBytecode.CompileFromFile(vertexShaderString, "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
              {
                  vertexShader = new VertexShader(device, bytecode);
                  inputSignature = ShaderSignature.GetInputSignature(bytecode);
              }
      
              inputLayout = inputLayoutMaker.MakeInputLayout(device,inputSignature);
          }
      
          protected abstract InputLayout MakeInputLayout(Device device, ShaderSignature inputSignature);
      }
      
      public class TextureShader:ShaderBase
      {
          private class TextureShaderInputLayoutMaker : IInputLayoutMaker
          {
              public InputLayout MakeInputLayout(Device device, SlimDX.D3DCompiler.ShaderSignature inputSignature)
              {
                  return new InputLayout(device, inputSignature, new[] { 
                      new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0), 
                      new InputElement("COLOR",0,SlimDX.DXGI.Format.R32G32B32_Float,0)
                  });
              }
          }
      
          public ColourShader(Device device) : base(device,"shaders/colour.fx", "shaders/colour.fx", new TextureShaderInputLayoutMaker())
          {
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.