| |
| <!doctype html> |
| <html lang="en" class="no-js"> |
| <head> |
| |
| <meta charset="utf-8"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| |
| |
| |
| <link rel="canonical" href="https://flatbuffers.dev/languages/c_sharp/"> |
| |
| |
| <link rel="prev" href="../cpp/"> |
| |
| |
| <link rel="next" href="../dart/"> |
| |
| |
| <link rel="icon" href="../../assets/images/favicon.png"> |
| <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.6.16"> |
| |
| |
| |
| <title>C# - FlatBuffers Docs</title> |
| |
| |
| |
| <link rel="stylesheet" href="../../assets/stylesheets/main.7e37652d.min.css"> |
| |
| |
| <link rel="stylesheet" href="../../assets/stylesheets/palette.06af60db.min.css"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> |
| <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"> |
| <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style> |
| |
| |
| |
| <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script> |
| |
| |
| |
| |
| |
| |
| </head> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo"> |
| |
| |
| <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off"> |
| <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off"> |
| <label class="md-overlay" for="__drawer"></label> |
| <div data-md-component="skip"> |
| |
| |
| <a href="#flatbuffers_guide_use_c-sharp" class="md-skip"> |
| Skip to content |
| </a> |
| |
| </div> |
| <div data-md-component="announce"> |
| |
| </div> |
| |
| |
| |
| |
| |
| |
| <header class="md-header md-header--shadow" data-md-component="header"> |
| <nav class="md-header__inner md-grid" aria-label="Header"> |
| <a href="../.." title="FlatBuffers Docs" class="md-header__button md-logo" aria-label="FlatBuffers Docs" data-md-component="logo"> |
| |
| <img src="../../assets/flatbuffers_logo.svg" alt="logo"> |
| |
| </a> |
| <label class="md-header__button md-icon" for="__drawer"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> |
| </label> |
| <div class="md-header__title" data-md-component="header-title"> |
| <div class="md-header__ellipsis"> |
| <div class="md-header__topic"> |
| <span class="md-ellipsis"> |
| FlatBuffers Docs |
| </span> |
| </div> |
| <div class="md-header__topic" data-md-component="header-topic"> |
| <span class="md-ellipsis"> |
| |
| C# |
| |
| </span> |
| </div> |
| </div> |
| </div> |
| |
| |
| <form class="md-header__option" data-md-component="palette"> |
| |
| |
| |
| |
| <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo" aria-label="Switch to dark mode" type="radio" name="__palette" id="__palette_0"> |
| |
| <label class="md-header__button md-icon" title="Switch to dark mode" for="__palette_1" hidden> |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> |
| </label> |
| |
| |
| |
| |
| |
| <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="indigo" data-md-color-accent="indigo" aria-label="Switch to light mode" type="radio" name="__palette" id="__palette_1"> |
| |
| <label class="md-header__button md-icon" title="Switch to light mode" for="__palette_0" hidden> |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> |
| </label> |
| |
| |
| </form> |
| |
| |
| |
| <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> |
| |
| |
| |
| |
| <div class="md-header__source"> |
| <a href="https://github.com/google/flatbuffers" title="Go to repository" class="md-source" data-md-component="source"> |
| <div class="md-source__icon md-icon"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path fill="currentColor" d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> |
| </div> |
| <div class="md-source__repository"> |
| google/FlatBuffers |
| </div> |
| </a> |
| </div> |
| |
| </nav> |
| |
| </header> |
| |
| <div class="md-container" data-md-component="container"> |
| |
| |
| |
| |
| |
| |
| <main class="md-main" data-md-component="main"> |
| <div class="md-main__inner md-grid"> |
| |
| |
| |
| <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" > |
| <div class="md-sidebar__scrollwrap"> |
| <div class="md-sidebar__inner"> |
| |
| |
| |
| |
| <nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0"> |
| <label class="md-nav__title" for="__drawer"> |
| <a href="../.." title="FlatBuffers Docs" class="md-nav__button md-logo" aria-label="FlatBuffers Docs" data-md-component="logo"> |
| |
| <img src="../../assets/flatbuffers_logo.svg" alt="logo"> |
| |
| </a> |
| FlatBuffers Docs |
| </label> |
| |
| <div class="md-nav__source"> |
| <a href="https://github.com/google/flatbuffers" title="Go to repository" class="md-source" data-md-component="source"> |
| <div class="md-source__icon md-icon"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path fill="currentColor" d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> |
| </div> |
| <div class="md-source__repository"> |
| google/FlatBuffers |
| </div> |
| </a> |
| </div> |
| |
| <ul class="md-nav__list" data-md-scrollfix> |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../.." class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Overview |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../quick_start/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Quick Start |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../tutorial/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Tutorial |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item md-nav__item--nested"> |
| |
| |
| |
| |
| |
| <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_4" > |
| |
| |
| <label class="md-nav__link" for="__nav_4" id="__nav_4_label" tabindex="0"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Compiler (flatc) |
| |
| </span> |
| |
| |
| <span class="md-nav__icon md-icon"></span> |
| </label> |
| |
| <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="false"> |
| <label class="md-nav__title" for="__nav_4"> |
| <span class="md-nav__icon md-icon"></span> |
| Compiler (flatc) |
| </label> |
| <ul class="md-nav__list" data-md-scrollfix> |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../building/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Building |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../flatc/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Using |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item md-nav__item--nested"> |
| |
| |
| |
| |
| |
| <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_5" > |
| |
| |
| <label class="md-nav__link" for="__nav_5" id="__nav_5_label" tabindex="0"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Schema (.fbs) |
| |
| </span> |
| |
| |
| <span class="md-nav__icon md-icon"></span> |
| </label> |
| |
| <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false"> |
| <label class="md-nav__title" for="__nav_5"> |
| <span class="md-nav__icon md-icon"></span> |
| Schema (.fbs) |
| </label> |
| <ul class="md-nav__list" data-md-scrollfix> |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../schema/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Overview |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../evolution/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Evolution |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../grammar/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Grammar |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item md-nav__item--active md-nav__item--nested"> |
| |
| |
| |
| <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_6" checked> |
| |
| |
| <label class="md-nav__link" for="__nav_6" id="__nav_6_label" tabindex="0"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Language Guides |
| |
| </span> |
| |
| |
| <span class="md-nav__icon md-icon"></span> |
| </label> |
| |
| <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="true"> |
| <label class="md-nav__title" for="__nav_6"> |
| <span class="md-nav__icon md-icon"></span> |
| Language Guides |
| </label> |
| <ul class="md-nav__list" data-md-scrollfix> |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../c/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| C |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../cpp/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| C++ |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item md-nav__item--active"> |
| |
| <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc"> |
| |
| |
| |
| |
| |
| <label class="md-nav__link md-nav__link--active" for="__toc"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| C# |
| |
| </span> |
| |
| |
| <span class="md-nav__icon md-icon"></span> |
| </label> |
| |
| <a href="./" class="md-nav__link md-nav__link--active"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| C# |
| |
| </span> |
| |
| |
| </a> |
| |
| |
| |
| <nav class="md-nav md-nav--secondary" aria-label="Table of contents"> |
| |
| |
| |
| |
| |
| |
| <label class="md-nav__title" for="__toc"> |
| <span class="md-nav__icon md-icon"></span> |
| Table of contents |
| </label> |
| <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> |
| |
| <li class="md-nav__item"> |
| <a href="#before-you-get-started" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Before you get started |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#flatbuffers-c-code-location" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| FlatBuffers C# code location |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#building-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Building the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#testing-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Testing the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#using-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Using the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#storing-dictionaries-in-a-flatbuffer" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Storing dictionaries in a FlatBuffer |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#buffer-verification" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Buffer verification |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#text-parsing" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Text parsing |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#object-based-api" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Object based API |
| </span> |
| </a> |
| |
| <nav class="md-nav" aria-label="Object based API"> |
| <ul class="md-nav__list"> |
| |
| <li class="md-nav__item"> |
| <a href="#json-serialization" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Json Serialization |
| </span> |
| </a> |
| |
| </li> |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#conditional-compilation-symbols" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Conditional compilation symbols |
| </span> |
| </a> |
| |
| </li> |
| |
| </ul> |
| |
| </nav> |
| |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../dart/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Dart |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../go/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Go |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../java/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Java |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../javascript/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| JavaScript |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../kotlin/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Kotlin |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../lobster/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Lobster |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../lua/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Lua |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../php/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| PHP |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../python/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Python |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../rust/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Rust |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../swift/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Swift |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../typescript/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| TypeScript |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../support/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Supported Configurations |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../white_paper/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| White Paper |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item md-nav__item--nested"> |
| |
| |
| |
| |
| |
| <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_9" > |
| |
| |
| <label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Advanced |
| |
| </span> |
| |
| |
| <span class="md-nav__icon md-icon"></span> |
| </label> |
| |
| <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_9_label" aria-expanded="false"> |
| <label class="md-nav__title" for="__nav_9"> |
| <span class="md-nav__icon md-icon"></span> |
| Advanced |
| </label> |
| <ul class="md-nav__list" data-md-scrollfix> |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../internals/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| FlatBuffers Internals |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../intermediate_representation/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Intermediate Representation |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../annotation/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Annotating Buffers (.afb) |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../benchmarks/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Benchmarks |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../flexbuffers/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| FlexBuffers (Schema-less version) |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <li class="md-nav__item"> |
| <a href="../../contributing/" class="md-nav__link"> |
| |
| |
| |
| <span class="md-ellipsis"> |
| Contributing |
| |
| </span> |
| |
| |
| </a> |
| </li> |
| |
| |
| |
| </ul> |
| </nav> |
| </div> |
| </div> |
| </div> |
| |
| |
| |
| <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" > |
| <div class="md-sidebar__scrollwrap"> |
| <div class="md-sidebar__inner"> |
| |
| |
| <nav class="md-nav md-nav--secondary" aria-label="Table of contents"> |
| |
| |
| |
| |
| |
| |
| <label class="md-nav__title" for="__toc"> |
| <span class="md-nav__icon md-icon"></span> |
| Table of contents |
| </label> |
| <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> |
| |
| <li class="md-nav__item"> |
| <a href="#before-you-get-started" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Before you get started |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#flatbuffers-c-code-location" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| FlatBuffers C# code location |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#building-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Building the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#testing-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Testing the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#using-the-flatbuffers-c-library" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Using the FlatBuffers C# library |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#storing-dictionaries-in-a-flatbuffer" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Storing dictionaries in a FlatBuffer |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#buffer-verification" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Buffer verification |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#text-parsing" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Text parsing |
| </span> |
| </a> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#object-based-api" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Object based API |
| </span> |
| </a> |
| |
| <nav class="md-nav" aria-label="Object based API"> |
| <ul class="md-nav__list"> |
| |
| <li class="md-nav__item"> |
| <a href="#json-serialization" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Json Serialization |
| </span> |
| </a> |
| |
| </li> |
| |
| </ul> |
| </nav> |
| |
| </li> |
| |
| <li class="md-nav__item"> |
| <a href="#conditional-compilation-symbols" class="md-nav__link"> |
| <span class="md-ellipsis"> |
| Conditional compilation symbols |
| </span> |
| </a> |
| |
| </li> |
| |
| </ul> |
| |
| </nav> |
| </div> |
| </div> |
| </div> |
| |
| |
| |
| <div class="md-content" data-md-component="content"> |
| <article class="md-content__inner md-typeset"> |
| |
| |
| |
| |
| |
| <a href="https://github.com/google/flatbuffers/edit/master/docs/source/languages/c_sharp.md" title="Edit this page" class="md-content__button md-icon" rel="edit"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M10 20H6V4h7v5h5v3.1l2-2V8l-6-6H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h4zm10.2-7c.1 0 .3.1.4.2l1.3 1.3c.2.2.2.6 0 .8l-1 1-2.1-2.1 1-1c.1-.1.2-.2.4-.2m0 3.9L14.1 23H12v-2.1l6.1-6.1z"/></svg> |
| </a> |
| |
| |
| |
| |
| <h1 id="flatbuffers_guide_use_c-sharp">Use in C#</h1> |
| <h2 id="before-you-get-started">Before you get started</h2> |
| <p>Before diving into the FlatBuffers usage in C#, it should be noted that |
| the <a href="../../tutorial/">Tutorial</a> page has a complete guide to |
| general FlatBuffers usage in all of the supported languages (including C#). |
| This page is designed to cover the nuances of FlatBuffers usage, |
| specific to C#.</p> |
| <p>You should also have read the <a href="../../building/">Building</a> |
| documentation to build <code>flatc</code> and should be familiar with |
| <a href="../../flatc/">Using the schema compiler</a> and |
| <a href="../../schema/">Writing a schema</a>.</p> |
| <h2 id="flatbuffers-c-code-location">FlatBuffers C# code location</h2> |
| <p>The code for the FlatBuffers C# library can be found at |
| <code>flatbuffers/net/FlatBuffers</code>. You can browse the library on the |
| <a href="https://github.com/google/flatbuffers/tree/master/net/ |
| FlatBuffers">FlatBuffers GitHub page</a>.</p> |
| <h2 id="building-the-flatbuffers-c-library">Building the FlatBuffers C# library</h2> |
| <p>The <code>FlatBuffers.csproj</code> project contains multitargeting for .NET Standard 2.1, |
| .NET 6 and .NET 8.</p> |
| <p>You can build for a specific framework target when using the cross-platform |
| <a href="https://dotnet.microsoft.com/download">.NET Core SDK</a> by adding the <code>-f</code> |
| command line option:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span>dotnet<span class="w"> </span>build<span class="w"> </span>-f<span class="w"> </span>netstandard2.1<span class="w"> </span><span class="s2">"FlatBuffers.csproj"</span> |
| </code></pre></div> |
| <p>The <code>FlatBuffers.csproj</code> project also provides support for defining various |
| conditional compilation symbols (see "Conditional compilation symbols" section |
| below) using the <code>-p</code> command line option:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span>dotnet<span class="w"> </span>build<span class="w"> </span>-f<span class="w"> </span>netstandard2.1<span class="w"> </span>-p:ENABLE_SPAN_T<span class="o">=</span><span class="nb">true</span><span class="w"> </span>-p:UNSAFE_BYTEBUFFER<span class="o">=</span><span class="nb">true</span><span class="w"> </span><span class="s2">"FlatBuffers.csproj"</span> |
| </code></pre></div> |
| <h2 id="testing-the-flatbuffers-c-library">Testing the FlatBuffers C# library</h2> |
| <p>The code to test the libraries can be found at <code>flatbuffers/tests</code>.</p> |
| <p>The test code for C# is located in the <a href="https://github.com/ |
| google/flatbuffers/tree/master/tests/FlatBuffers.Test">FlatBuffers.Test</a> subfolder. To run the |
| tests, open <code>FlatBuffers.Test.csproj</code> in <a href="https://www.visualstudio.com">Visual Studio</a>, and compile/run the project.</p> |
| <p>Optionally, you can run this using <a href="http://www.mono-project.com/">Mono</a> instead. |
| Once you have installed Mono, you can run the tests from the command line |
| by running the following commands from inside the <code>FlatBuffers.Test</code> folder:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span>mcs<span class="w"> </span>*.cs<span class="w"> </span>../MyGame/Example/*.cs<span class="w"> </span>../../net/FlatBuffers/*.cs |
| <span class="w"> </span>mono<span class="w"> </span>Assert.exe |
| </code></pre></div> |
| <h2 id="using-the-flatbuffers-c-library">Using the FlatBuffers C# library</h2> |
| <p><em>Note: See <a href="../../tutorial/">Tutorial</a> for a more in-depth |
| example of how to use FlatBuffers in C#.</em></p> |
| <p>FlatBuffers supports reading and writing binary FlatBuffers in C#.</p> |
| <p>To use FlatBuffers in your own code, first generate C# classes from your |
| schema with the <code>--csharp</code> option to <code>flatc</code>. |
| Then you can include both FlatBuffers and the generated code to read |
| or write a FlatBuffer.</p> |
| <p>For example, here is how you would read a FlatBuffer binary file in C#: |
| First, import the library and generated code. Then, you read a FlatBuffer binary |
| file into a <code>byte[]</code>. You then turn the <code>byte[]</code> into a <code>ByteBuffer</code>, which you |
| pass to the <code>GetRootAsMyRootType</code> function:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="nn">MyGame.Example</span><span class="p">;</span> |
| <span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="nn">Google.FlatBuffers</span><span class="p">;</span> |
| |
| <span class="w"> </span><span class="c1">// This snippet ignores exceptions for brevity.</span> |
| <span class="w"> </span><span class="kt">byte</span><span class="p">[]</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">File</span><span class="p">.</span><span class="n">ReadAllBytes</span><span class="p">(</span><span class="s">"monsterdata_test.mon"</span><span class="p">);</span> |
| |
| <span class="w"> </span><span class="n">ByteBuffer</span><span class="w"> </span><span class="n">bb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ByteBuffer</span><span class="p">(</span><span class="n">data</span><span class="p">);</span> |
| <span class="w"> </span><span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">GetRootAsMonster</span><span class="p">(</span><span class="n">bb</span><span class="p">);</span> |
| </code></pre></div> |
| <p>Now you can access the data from the <code>Monster monster</code>:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="kt">short</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Hp</span><span class="p">;</span> |
| <span class="w"> </span><span class="n">Vec3</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Pos</span><span class="p">;</span> |
| </code></pre></div> |
| <p>C# code naming follows standard C# style with PascalCasing identifiers, |
| e.g. <code>GetRootAsMyRootType</code>. Also, values (except vectors and unions) are |
| available as properties instead of parameterless accessor methods. |
| The performance-enhancing methods to which you can pass an already created |
| object are prefixed with <code>Get</code>, e.g.:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="c1">// property</span> |
| <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Pos</span><span class="p">;</span> |
| |
| <span class="w"> </span><span class="c1">// method filling a preconstructed object</span> |
| <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">preconstructedPos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Vec3</span><span class="p">();</span> |
| <span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">GetPos</span><span class="p">(</span><span class="n">preconstructedPos</span><span class="p">);</span> |
| </code></pre></div> |
| <h2 id="storing-dictionaries-in-a-flatbuffer">Storing dictionaries in a FlatBuffer</h2> |
| <p>FlatBuffers doesn't support dictionaries natively, but there is support to |
| emulate their behavior with vectors and binary search, which means you |
| can have fast lookups directly from a FlatBuffer without having to unpack |
| your data into a <code>Dictionary</code> or similar.</p> |
| <p>To use it: |
| - Designate one of the fields in a table as the "key" field. You do this |
| by setting the <code>key</code> attribute on this field, e.g. |
| <code>name:string (key)</code>. |
| You may only have one key field, and it must be of string or scalar type. |
| - Write out tables of this type as usual, collect their offsets in an |
| array. |
| - Instead of calling standard generated method, |
| e.g.: <code>Monster.createTestarrayoftablesVector</code>, |
| call <code>CreateSortedVectorOfMonster</code> in C# |
| which will first sort all offsets such that the tables they refer to |
| are sorted by the key field, then serialize it. |
| - Now when you're accessing the FlatBuffer, you can use |
| the <code>ByKey</code> accessor to access elements of the vector, e.g.: |
| <code>monster.TestarrayoftablesByKey("Frodo")</code> in C#, |
| which returns an object of the corresponding table type, |
| or <code>null</code> if not found. |
| <code>ByKey</code> performs a binary search, so should have a similar |
| speed to <code>Dictionary</code>, though may be faster because of better caching. |
| <code>ByKey</code> only works if the vector has been sorted, it will |
| likely not find elements if it hasn't been sorted.</p> |
| <h2 id="buffer-verification">Buffer verification</h2> |
| <p>As mentioned in <a href="../cpp/">C++ Usage</a> buffer |
| accessor functions do not verify buffer offsets at run-time. |
| If it is necessary, you can optionally use a buffer verifier before you |
| access the data. This verifier will check all offsets, all sizes of |
| fields, and null termination of strings to ensure that when a buffer |
| is accessed, all reads will end up inside the buffer.</p> |
| <p>Each root type will have a verification function generated for it, |
| e.g. <code>Monster.VerifyMonster</code>. This can be called as shown: |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">VerifyMonster</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span> |
| </code></pre></div> |
| if <code>ok</code> is true, the buffer is safe to read.</p> |
| <p>For a more detailed control of verification <code>MonsterVerify.Verify</code> |
| for <code>Monster</code> type can be used: |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">Sequence</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">calls</span> |
| <span class="w"> </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="w"> </span><span class="n">verifier</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span> |
| <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">verifier</span><span class="p">.</span><span class="n">VerifyBuffer</span><span class="p">(</span><span class="s">"MONS"</span><span class="p">,</span><span class="w"> </span><span class="k">false</span><span class="p">,</span><span class="w"> </span><span class="n">MonsterVerify</span><span class="p">.</span><span class="n">Verify</span><span class="p">);</span> |
| |
| <span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="n">Or</span><span class="w"> </span><span class="n">single</span><span class="w"> </span><span class="n">line</span><span class="w"> </span><span class="n">call</span> |
| <span class="w"> </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="p">(</span><span class="n">bb</span><span class="p">).</span><span class="n">setStringCheck</span><span class="p">(</span><span class="k">true</span><span class="p">).</span>\ |
| <span class="w"> </span><span class="n">VerifyBuffer</span><span class="p">(</span><span class="s">"MONS"</span><span class="p">,</span><span class="w"> </span><span class="k">false</span><span class="p">,</span><span class="w"> </span><span class="n">MonsterVerify</span><span class="p">.</span><span class="n">Verify</span><span class="p">);</span> |
| </code></pre></div> |
| if <code>ok</code> is true, the buffer is safe to read.</p> |
| <p>A second parameter of <code>verifyBuffer</code> specifies whether buffer content is |
| size prefixed or not. In the example above, the buffer is assumed to not include |
| size prefix (<code>false</code>).</p> |
| <p>Verifier supports options that can be set using appropriate fluent methods: |
| * SetMaxDepth - limit the nesting depth. Default: 1000000 |
| * SetMaxTables - total amount of tables the verifier may encounter. Default: 64 |
| * SetAlignmentCheck - check content alignment. Default: True |
| * SetStringCheck - check if strings contain termination '0' character. Default: true</p> |
| <h2 id="text-parsing">Text parsing</h2> |
| <p>There currently is no support for parsing text (Schema's and JSON) directly |
| from C#, though you could use the C++ parser through native call |
| interfaces available to each language. Please see the |
| C++ documentation for more on text parsing.</p> |
| <h2 id="object-based-api">Object based API</h2> |
| <p>FlatBuffers is all about memory efficiency, which is why its base API is written |
| around using as little as possible of it. This does make the API clumsier |
| (requiring pre-order construction of all data, and making mutation harder).</p> |
| <p>For times when efficiency is less important a more convenient object based API |
| can be used (through <code>--gen-object-api</code>) that is able to unpack & pack a |
| FlatBuffer into objects and standard <code>System.Collections.Generic</code> containers, |
| allowing for convenient construction, access and mutation.</p> |
| <p>To use:</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="c1">// Deserialize from buffer into object.</span> |
| <span class="w"> </span><span class="n">MonsterT</span><span class="w"> </span><span class="n">monsterobj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetMonster</span><span class="p">(</span><span class="n">flatbuffer</span><span class="p">).</span><span class="n">UnPack</span><span class="p">();</span> |
| |
| <span class="w"> </span><span class="c1">// Update object directly like a C# class instance.</span> |
| <span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">monsterobj</span><span class="p">.</span><span class="n">Name</span><span class="p">);</span> |
| <span class="w"> </span><span class="n">monsterobj</span><span class="p">.</span><span class="n">Name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Bob"</span><span class="p">;</span><span class="w"> </span><span class="c1">// Change the name.</span> |
| |
| <span class="w"> </span><span class="c1">// Serialize into new flatbuffer.</span> |
| <span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="w"> </span><span class="n">fbb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> |
| <span class="w"> </span><span class="n">fbb</span><span class="p">.</span><span class="n">Finish</span><span class="p">(</span><span class="n">Monster</span><span class="p">.</span><span class="n">Pack</span><span class="p">(</span><span class="n">fbb</span><span class="p">,</span><span class="w"> </span><span class="n">monsterobj</span><span class="p">).</span><span class="n">Value</span><span class="p">);</span> |
| </code></pre></div> |
| <h3 id="json-serialization">Json Serialization</h3> |
| <p>An additional feature of the object API is the ability to allow you to |
| serialize & deserialize a JSON text. |
| To use Json Serialization, add <code>--cs-gen-json-serializer</code> option to <code>flatc</code> and |
| add <code>Newtonsoft.Json</code> nuget package to csproj. This requires explicitly setting |
| the <code>--gen-object-api</code> option as well.</p> |
| <div class="highlight"><pre><span></span><code><span class="w"> </span><span class="c1">// Deserialize MonsterT from json</span> |
| <span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="n">jsonText</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="s">@"Resources/monsterdata_test.json"</span><span class="p">);</span> |
| <span class="w"> </span><span class="n">MonsterT</span><span class="w"> </span><span class="n">mon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MonsterT</span><span class="p">.</span><span class="n">DeserializeFromJson</span><span class="p">(</span><span class="n">jsonText</span><span class="p">);</span> |
| |
| <span class="w"> </span><span class="c1">// Serialize MonsterT to json</span> |
| <span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="n">jsonText2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mon</span><span class="p">.</span><span class="n">SerializeToJson</span><span class="p">();</span> |
| </code></pre></div> |
| <ul> |
| <li>Limitation</li> |
| <li><code>hash</code> attribute currently not supported.</li> |
| <li>NuGet package Dependency</li> |
| <li><a href="https://github.com/JamesNK/Newtonsoft.Json">Newtonsoft.Json</a></li> |
| </ul> |
| <h2 id="conditional-compilation-symbols">Conditional compilation symbols</h2> |
| <p>There are three conditional compilation symbols that have an impact on |
| performance/features of the C# <code>ByteBuffer</code> implementation.</p> |
| <ul> |
| <li><code>UNSAFE_BYTEBUFFER</code></li> |
| </ul> |
| <p>This will use unsafe code to manipulate the underlying byte array. This can |
| yield a reasonable performance increase.</p> |
| <ul> |
| <li><code>BYTEBUFFER_NO_BOUNDS_CHECK</code></li> |
| </ul> |
| <p>This will disable the bounds check asserts to the byte array. This can yield a |
| small performance gain in normal code.</p> |
| <ul> |
| <li><code>ENABLE_SPAN_T</code></li> |
| </ul> |
| <p>This will enable reading and writing blocks of memory with a <code>Span<T></code> instead |
| of just <code>T[]</code>. You can also enable writing directly to shared memory or other |
| types of memory by providing a custom implementation of <code>ByteBufferAllocator</code>. |
| <code>ENABLE_SPAN_T</code> also requires <code>UNSAFE_BYTEBUFFER</code> to be defined, or .NET |
| Standard 2.1.</p> |
| <p>Using <code>UNSAFE_BYTEBUFFER</code> and <code>BYTEBUFFER_NO_BOUNDS_CHECK</code> together can yield a |
| performance gain of ~15% for some operations, however doing so is potentially |
| dangerous. Do so at your own risk!</p> |
| <p><br></p> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| </article> |
| </div> |
| |
| |
| <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> |
| |
| <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> |
| </div> |
| |
| </main> |
| |
| <footer class="md-footer"> |
| |
| |
| |
| <nav class="md-footer__inner md-grid" aria-label="Footer" > |
| |
| |
| <a href="../cpp/" class="md-footer__link md-footer__link--prev" aria-label="Previous: C++"> |
| <div class="md-footer__button md-icon"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg> |
| </div> |
| <div class="md-footer__title"> |
| <span class="md-footer__direction"> |
| Previous |
| </span> |
| <div class="md-ellipsis"> |
| C++ |
| </div> |
| </div> |
| </a> |
| |
| |
| |
| <a href="../dart/" class="md-footer__link md-footer__link--next" aria-label="Next: Dart"> |
| <div class="md-footer__title"> |
| <span class="md-footer__direction"> |
| Next |
| </span> |
| <div class="md-ellipsis"> |
| Dart |
| </div> |
| </div> |
| <div class="md-footer__button md-icon"> |
| |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z"/></svg> |
| </div> |
| </a> |
| |
| </nav> |
| |
| |
| <div class="md-footer-meta md-typeset"> |
| <div class="md-footer-meta__inner md-grid"> |
| <div class="md-copyright"> |
| |
| <div class="md-copyright__highlight"> |
| Copyright © 2025 Google |
| </div> |
| |
| |
| Made with |
| <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener"> |
| Material for MkDocs |
| </a> |
| |
| </div> |
| |
| <div class="md-social"> |
| |
| |
| |
| |
| |
| |
| |
| |
| <a href="https://github.com/google/flatbuffers" target="_blank" rel="noopener" title="github.com" class="md-social__link"> |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path fill="currentColor" d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> |
| </a> |
| |
| |
| |
| |
| |
| |
| |
| |
| <a href="https:///discord.gg/6qgKs3R" target="_blank" rel="noopener" title="" class="md-social__link"> |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path fill="currentColor" d="M492.5 69.8c-.2-.3-.4-.6-.8-.7-38.1-17.5-78.4-30-119.7-37.1-.4-.1-.8 0-1.1.1s-.6.4-.8.8c-5.5 9.9-10.5 20.2-14.9 30.6-44.6-6.8-89.9-6.8-134.4 0-4.5-10.5-9.5-20.7-15.1-30.6-.2-.3-.5-.6-.8-.8s-.7-.2-1.1-.2C162.5 39 122.2 51.5 84.1 69c-.3.1-.6.4-.8.7C7.1 183.5-13.8 294.6-3.6 404.2c0 .3.1.5.2.8s.3.4.5.6c44.4 32.9 94 58 146.8 74.2.4.1.8.1 1.1 0s.7-.4.9-.7c11.3-15.4 21.4-31.8 30-48.8.1-.2.2-.5.2-.8s0-.5-.1-.8-.2-.5-.4-.6-.4-.3-.7-.4c-15.8-6.1-31.2-13.4-45.9-21.9-.3-.2-.5-.4-.7-.6s-.3-.6-.3-.9 0-.6.2-.9.3-.5.6-.7c3.1-2.3 6.2-4.7 9.1-7.1.3-.2.6-.4.9-.4s.7 0 1 .1c96.2 43.9 200.4 43.9 295.5 0 .3-.1.7-.2 1-.2s.7.2.9.4c2.9 2.4 6 4.9 9.1 7.2.2.2.4.4.6.7s.2.6.2.9-.1.6-.3.9-.4.5-.6.6c-14.7 8.6-30 15.9-45.9 21.8-.2.1-.5.2-.7.4s-.3.4-.4.7-.1.5-.1.8.1.5.2.8c8.8 17 18.8 33.3 30 48.8.2.3.6.6.9.7s.8.1 1.1 0c52.9-16.2 102.6-41.3 147.1-74.2.2-.2.4-.4.5-.6s.2-.5.2-.8c12.3-126.8-20.5-236.9-86.9-334.5zm-302 267.7c-29 0-52.8-26.6-52.8-59.2s23.4-59.2 52.8-59.2c29.7 0 53.3 26.8 52.8 59.2 0 32.7-23.4 59.2-52.8 59.2m195.4 0c-29 0-52.8-26.6-52.8-59.2s23.4-59.2 52.8-59.2c29.7 0 53.3 26.8 52.8 59.2 0 32.7-23.2 59.2-52.8 59.2"/></svg> |
| </a> |
| |
| |
| |
| |
| |
| |
| |
| |
| <a href="https://twitter.com/dbaileychess" target="_blank" rel="noopener" title="twitter.com" class="md-social__link"> |
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path fill="currentColor" d="M357.2 48h70.6L273.6 224.2 455 464H313L201.7 318.6 74.5 464H3.8l164.9-188.5L-5.2 48h145.6l100.5 132.9zm-24.8 373.8h39.1L119.1 88h-42z"/></svg> |
| </a> |
| |
| </div> |
| |
| </div> |
| </div> |
| </footer> |
| |
| </div> |
| <div class="md-dialog" data-md-component="dialog"> |
| <div class="md-dialog__inner md-typeset"></div> |
| </div> |
| |
| |
| |
| |
| <script id="__config" type="application/json">{"base": "../..", "features": ["content.code.annotate", "content.tabs.link", "navigation.expand", "navigation.footer", "header.autohide", "content.action.edit"], "search": "../../assets/javascripts/workers/search.d50fe291.min.js", "tags": null, "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}, "version": null}</script> |
| |
| |
| <script src="../../assets/javascripts/bundle.50899def.min.js"></script> |
| |
| |
| </body> |
| </html> |