Announcement

Collapse
No announcement yet.

[Field] attribute not work when converting C# property to js property

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [Field] attribute not work when converting C# property to js property

    C# class with property like this, seemed cannot be converted.


    [AttributeUsage(AttributeTargets.Property)]
    public sealed class FieldAttribute : Attribute
    {
    public FieldAttribute()
    {
    }
    }

    class TestPropertyConvert
    {
    public int m_testSelfSeat;

    public int TestMySeat { get; set; }

    [Field]
    public int TestSelfSeat
    {
    set
    {
    m_testSelfSeat = value;
    }

    get
    {
    return m_testSelfSeat;
    }
    }
    }
    when using converted js, cannot use TestSelfSeat like a property:
    let testConvert = new TestPropertyConvert();
    testConvert.TestSelfSeat = 23; // m_testSelfSeat is not set to 23
    Can anybody know how to convert C# properties above to js, so that I can use like js properties?

    p.s.
    C# properties defined like below can be converted to js properties ok.
    public int TestMySeat { get; set; }

    #2
    If you are using Bridge 15.7 or lower, please use [FieldProperty] instead of [Field]. As of Bridge 16.0, please use [Field].

    Comment


      #3
      @geffrey.mcgill, thanks for your advice, but an error popped when compiling with [FieldProperty] attribute, maybe I should try version 16.0 and above?

      Error Error: [Property TestPropertyConvert.TestSelfSeat] is marked with [FieldProperty] attribute but implements getter and setter. To fix the problem either remove [FieldProperty] (swith off bridge.json option `autoPropertyToField`) or add [External]/[Template] attributes at Bridge.Translator.Inspector.CheckFieldProperty(Pro pertyDeclaration propertyDeclaration, Boolean& isResolvedProperty, MemberResolveResult& resolvedProperty)
      at Bridge.Translator.Inspector.VisitPropertyDeclarati on(PropertyDeclaration propertyDeclaration)
      at ICSharpCode.NRefactory.CSharp.PropertyDeclaration. AcceptVisitor(IAstVisitor visitor)
      at Bridge.Contract.Helpers.AcceptChildren(AstNode node, IAstVisitor visitor)
      at Bridge.Translator.Inspector.VisitTypeDeclaration(T ypeDeclaration typeDeclaration)
      at ICSharpCode.NRefactory.CSharp.TypeDeclaration.Acce ptVisitor(IAstVisitor visitor)
      at Bridge.Contract.Helpers.AcceptChildren(AstNode node, IAstVisitor visitor)
      at Bridge.Translator.Inspector.VisitSyntaxTree(Syntax Tree node)
      at Bridge.Translator.Translator.InspectTypes(MemberRe solver resolver, IAssemblyInfo config)
      at Bridge.Translator.Translator.Translate()
      at Bridge.Build.GenerateScript.Execute() CSharpStuff D:\work\out_hlmjh5\jsconverter\CSharpStuff\CSharpS tuff\Stuff\Unity\TestPropertyConvert.cs 13

      Comment


        #4
        Can you link us to a Deck.NET sample demonstrating how to reproduce?

        Comment


          #5
          geoffrey.mcgill, Just copy the following codes into Deck.Net C# box, and check the result:
          public class Program
          {
          public int m_testSelfSeat;

          [FieldProperty]
          public int TestSelfSeat
          {
          set
          {
          m_testSelfSeat = value;
          }

          get
          {
          return m_testSelfSeat;
          }
          }

          public static void Main()
          {
          var msg = "Hello, World!";

          Console.WriteLine(msg);
          }
          }
          Last edited by gerrysun; 2017-03-05 @ 11:45 PM.

          Comment


            #6
            Can anybody know how to convert C# properties above to js, so that I can use like js properties?
            Are you referring to using Object.defineProperty to declare proper JavaScript setters and getters?

            https://developer.mozilla.org/en-US/...defineProperty

            If yes to the above question, Issue #1229 is tracking this functionality.

            If you want to treat TestSelfSeat as a JavaScript field, then the [Field] (formally [FieldProperty]) only makes sense in a scenario where properties are defined as auto-implemented properties.

            Comment


              #7
              geoffrey.mcgill , thanks for your information, seemed 16.0 will solve this issue, will try later.

              Comment

              Working...
              X