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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:37:49+00:00 2026-06-15T22:37:49+00:00

I would like to know if it’s possible to create a method that will

  • 0

I would like to know if it’s possible to create a method that will update a property/field via reflection simply by passing in the names of the property/field to set, i.e.

IFTDGN.Set( "S009", "E1001", "B" );

Where S009 is a field contained within the IFTDGN class, and E1001 is a field/property accessed through the S009 field, and finally B is the value I want to put into the E1001 property/field.

I have this code (to make it easy to reproduce):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EDIDemo
{
    using System.Reflection;

    class Program
    {
        static void Main( string[] args )
        {
            IFTDGN iftdgn = new IFTDGN();
            iftdgn.S009.E1001.Set( "A" );
            iftdgn.S009.E1002.Set( "CC" );

            iftdgn.Set( "S009", "E1001", "B" );

            Console.WriteLine( iftdgn.CreateMessage() );
            Console.ReadKey();
        }
    }

    /// <summary>
    ///  Represents an International Forwarding and Transport Dangerous Goods Notification message 
    /// </summary>
    class IFTDGN : EDIMessage
    {
        public S009 S009 = new S009();

        public IFTDGN()
            : base()
        {
            base.RegisterMessage( this.GetType().Name );
        }

        public override string CreateMessage()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine( "S009" );
            sb.AppendFormat( "\t{0} {1}\n", "E1001", S009.E1001.Code.Tag );
            sb.AppendFormat( "\t{0} {1}\n", "E1002", S009.E1002.Code.Tag );

            return sb.ToString();
        }

        public override void Set( string segment, string element, string code )
        {
            // *** THIS IS THE BIT I AM HAVING TROUBLE WITH **
            var fi_seg = this.GetType().GetFields().FirstOrDefault( p => p.Name == segment );
            var pi_elem = fi_seg.GetType().GetProperties().FirstOrDefault( p => p.Name == element );
        }

    }

    class S009
    {
        protected IDictionary<String, EDIDataElement> Elements = new Dictionary<String, EDIDataElement>()
        {
            { "E1001", new E1001() },
            { "E1002", new E1002() }
        };

        public E1001 E1001 { get { return (E1001)Elements[ "E1001" ]; } }
        public E1002 E1002 { get { return (E1002)Elements[ "E1002" ]; } }
    }

    abstract class EDIMessage
    {
        protected String MessageID;

        protected EDIMessage()
        { }

        protected void RegisterMessage( String messageID )
        {
            this.MessageID = messageID;
        }

        public virtual void Set( String segment, String element, String code )
        {
            throw new NotImplementedException();
        }

        public abstract String CreateMessage();

    }

    abstract class EDIDataElement
    {
        public Code Code { get; private set; }

        public virtual bool Mandatory
        {
            get
            {
                return false;
            }
        }

        protected IList<Code> Codes = new List<Code>();
        protected String ElementID;

        public virtual void Set( String code )
        {
            if ( !Codes.Any( c => c.Tag == code ) )
                throw new Exception( "Code '" + code + "' not found for element '" + ElementID + "'" );

            this.Code = Codes.FirstOrDefault( c => c.Tag == code );
        }

        protected void RegisterElement( String elementID )
        {
            this.ElementID = elementID;
        }

    }

    class E1001 : EDIDataElement
    {
        public E1001()
            : base()
        {
            base.Codes.Add( new Code( "A", "Code A" ) );
            base.Codes.Add( new Code( "B", "Code B" ) );
            base.Codes.Add( new Code( "C", "Code C" ) );
            base.Codes.Add( new Code( "D", "Code D" ) );
            base.Codes.Add( new Code( "E", "Code E" ) );

            base.RegisterElement( this.GetType().Name );
        }

        public override void Set( String code )
        {
            base.Set( code );
        }

        public override bool Mandatory
        {
            get
            {
                return true;
            }
        }

    }

    class E1002 : EDIDataElement
    {
        public E1002()
            : base()
        {
            base.Codes.Add( new Code( "AA", "Code AA" ) );
            base.Codes.Add( new Code( "BB", "Code BB" ) );
            base.Codes.Add( new Code( "CC", "Code CC" ) );
            base.Codes.Add( new Code( "DD", "Code DD" ) );
            base.Codes.Add( new Code( "EE", "Code EE" ) );

            base.RegisterElement( this.GetType().Name );

        }

        public override void Set( String code )
        {
            base.Set( code );
        }

        public override bool Mandatory
        {
            get
            {
                return true;
            }
        }

    }

    class Code
    {
        public string Tag { get; protected set; }
        public string Name { get; protected set; }

        public Code( string tag, string name )
        {
            Tag = tag;
            Name = name;
        }
    }

}

Basically, I want the pi_elem variable to contain the PropertyInfo or FieldInfo for the E1001 referenced property/field, but it always returns null.

  • 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-15T22:37:50+00:00Added an answer on June 15, 2026 at 10:37 pm

    I guess

     var pi_elem = fi_seg.GetType().GetProperties().FirstOrDefault( p => p.Name == element );
    

    might be :

     var pi_elem = fi_seg.FieldType.GetProperties().FirstOrDefault( p => p.Name == element );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like know if it is possible to create embedding of audio message
Would like to know if it is possible to create a MessageBox with custom
Would like to know is that possible to have filter with default value with
I would like to know that how we can check the available memory in
i would like know how to build a rss feed that is evergoing using
I would like know the limit of maximum number of rows that can be
Would like to know if there is any resource on the web that conveniently
Would like to know if there is a function in python that can take
I would like to know is it possible from this table ID Price ServiceID
I would like know what is the best possible way to implement transactions with

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.