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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:33:02+00:00 2026-05-27T15:33:02+00:00

Goal I’m trying to load a custom class that contains a Texture2D from an

  • 0

Goal

I’m trying to load a custom class that contains a Texture2D from an xml file using the default importer (XML Content), with no processor.


Approach

Lots of research online and a lot of dealing with other errors lead me to this XML:

<?xml version="1.0" encoding="utf-16"?>
<XnaContent xmlns:Components="Entities.Components">
  <Asset Type="EntitiesContentPipeline.EntityTemplateContent">
    <Name>entity name</Name>
    <TestTexture>
      <Reference>#External1</Reference>
    </TestTexture>
  </Asset>
  <ExternalReferences>
    <ExternalReference ID="#External1" TargetType="Microsoft.Xna.Framework.Graphics.Texture2D">C:\Documents and Settings\GDuckett\My Documents\Visual Studio 2010\Projects\Gravitron\Gravitron\Gravitron\bin\x86\Debug\Content\Bullet.xnb</ExternalReference>
  </ExternalReferences>
</XnaContent>

Yes, i don’t like the hard-coded path either, but if i can get this working without a custom reader and or writer for each type containing a Texture2D i can live with it.

Below is my content version of the class (used by the pipeline):

[ContentSerializerRuntimeType("Entities.Content.EntityTemplate, Entities")]
public class EntityTemplateContent
{
    public string Name;
    public ExternalReference<Texture2D> TestTexture;

    public EntityTemplateContent()
    {

    }
}

Below is my runtime version:

public class EntityTemplate
{
    public string Name;
    public Texture2D TestTexture;

    public EntityTemplate()
    {

    }
}

Problem

If i try and do var test = Content.Load<EntityTemplate>("BulletTemplate"); below is the error i get:

Error loading “Bullet”. ContentTypeReader Microsoft.Xna.Framework.Content.Texture2DReader, Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553 conflicts with existing handler Microsoft.Xna.Framework.Content.ReflectiveReader`1[[Microsoft.Xna.Framework.Graphics.Texture2D, Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553]], Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553 for type Microsoft.Xna.Framework.Graphics.Texture2D.

It looks like the runtime reader found 2 readers for dealing with a Texture2D asset, the ReflectiveReader<Texture2D> reader and the Texture2DReader.


Question

How can i solve this problem, so i end up with an object correctly populated, with the Texture2D property referencing a loaded texture?

Note: I don’t want to add another string property and create a method on my object called LoadContent or something. I’d like to have Content.Load be the only thing i need to call.

I also want to avoid writing my own readers / writers for every type that contains a Texture2D property.

Ideally I want to avoid creating a wrapper class for Texture2D, or a subclass but if there’s no alternative then I’m happy for a solution that does this.

  • 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-27T15:33:03+00:00Added an answer on May 27, 2026 at 3:33 pm

    The exact error message was caused by having another Texture2D field in the content object.

    The overall problem of getting a reference to a runtime type from an ExternalReference<T> in a content type was solved with the below.

    Currently it’s really a proof-of-concept class, as such it works for the classes I’ve thrown at it so far, but will probably crash with anything more complicated.

    It uses reflection to convert any fields or properties of the input that are ExternalReference<T>‘s into built versions of the type requested by creating an appropriate version of ContentProcessorContext.BuildAsset<T,T> and invoking it. It recurses down the object tree to do the same for references to other objects.

    [ContentProcessor(DisplayName = "ExternalRefObjectContentProcessor")]
    public class ExternalRefObjectContentProcessor : ContentProcessor<object, object>
    {
        private void ReplaceReferences(object input, ContentProcessorContext context)
        {
            Func<ExternalReference<object>, string, object> BuildAssetMethodTemplate = context.BuildAsset<object, object>;
            var BuildAssetMethod = BuildAssetMethodTemplate.Method.GetGenericMethodDefinition();
    
            foreach (var field in input.GetType().GetFields().Where(f => !f.IsStatic && !f.IsLiteral))
            {
                Type fieldType = field.FieldType;
                object fieldValue = field.GetValue(input);
    
                if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(ExternalReference<>))
                {
                    var GenericBuildMethod = BuildAssetMethod.MakeGenericMethod(fieldType.GetGenericArguments().First(), fieldType.GetGenericArguments().First());
    
                    object BuiltObject;
    
                    try
                    {
                        BuiltObject = GenericBuildMethod.Invoke(context, new object[] { fieldValue, null });
                    }
                    catch (Exception Ex)
                    {
                        throw Ex.InnerException;
                    }
    
                    field.SetValue(input, BuiltObject);
                }
                else if (fieldValue is IEnumerable && !(fieldValue is string))
                {
                    foreach (var item in (fieldValue as IEnumerable))
                    {
                        ReplaceReferences(item, context);
                    }
                }
                else if (fieldValue != null && !(fieldValue is string))
                {
                    ReplaceReferences(fieldValue, context);
                }
            }
    
            foreach (var property in input.GetType().GetProperties().Where(p => p.CanRead && p.CanWrite))
            {
                Type propertyType = property.PropertyType;
                object propertyValue = property.GetValue(input, null);
    
                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(ExternalReference<>))
                {
                    var GenericBuildMethod = BuildAssetMethod.MakeGenericMethod(propertyType.GetGenericArguments().First(), propertyType.GetGenericArguments().First());
    
                    object BuiltObject;
    
                    try
                    {
                        BuiltObject = GenericBuildMethod.Invoke(context, new object[] { property.GetValue(input, null), null });
                    }
                    catch (Exception Ex)
                    {
                        throw Ex.InnerException;
                    }
                    property.SetValue(input, BuiltObject, null);
                }
                else if (propertyValue is IEnumerable && !(propertyValue is string))
                {
                    foreach (var item in (propertyValue as IEnumerable))
                    {
                        ReplaceReferences(item, context);
                    }
                }
                else if (propertyValue != null && !(propertyValue is string))
                {
                    ReplaceReferences(propertyValue, context);
                }
            }
        }
    
        public override object Process(object input, ContentProcessorContext context)
        {
            ReplaceReferences(input, context);
    
            return input;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to set the users notification sound from a file that is
My goal is to maintain a web file server separately from my main ASP.NET
Goal: Provide a web service using Visual Basic or C# or .NET that interacts
Goal: To render a google map using geolocation. I am trying to implement the
Goal is to replace 'byebye' with 'Hello ' # START: file.txt contains string byebye
Goal Take a class named Item and output its serialized XML as: <Template><!--some properties
GOAL My goal is to find a text file or library that enables me
Goal: to find count of all words in a file. file contains 1000+ words
Goal When the user clicks an element (with class="menu" ), the default action should
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do

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.