惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
S
SegmentFault 最新的问题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Project Zero
Project Zero
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
Recorded Future
Recorded Future
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
Schneier on Security
Schneier on Security
H
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
M
MIT News - Artificial intelligence
W
WeLiveSecurity
P
Proofpoint News Feed
A
About on SuperTechFans
S
Securelist
I
InfoQ
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 叶小钗
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
腾讯CDC
Jina AI
Jina AI
S
Schneier on Security
I
Intezer
V
Visual Studio Blog
美团技术团队
V2EX - 技术
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
罗磊的独立博客
Y
Y Combinator Blog

博客园 - kingBook

git patch git 修改最后一次提交的日期 Win10 修改特定格式文件的右键快捷菜单 TypeScript async、 await、Promise LayaAir3.x 侦听程序退出 旋转力学例子 旋转力学公式 凹多边形碰撞检测 LayaAir3.x 侦听键盘事件 URP 阴影 TypeScript 里的 override TypeScript 类的自身类型 Unity 二维数组序列化 Cocos Creator 安卓模拟器中无法运行 Unity Editor 保存图片、缩放纹理 LayaAir3.2.0-beta.2 设置2d刚体线性速度,在不同设备(分辨率)下,表现不一致的问题 LayaAir3.x 物理2D碰撞事件 TypeScirpt 声明Map类型变量 TypeScript 声明函数类型变量
C# 匿名对象、动态属性
kingBook · 2024-09-30 · via 博客园 - kingBook

以下代码基于 Unity, 放置在 Editor文件夹下

#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

public class EditorTest : Editor {

    [MenuItem("Tools/EditorTest", true)]
    private static bool ValidateMenuItem() {
        return !EditorApplication.isPlaying;
    }

    [MenuItem("Tools/EditorTest")]
    private static void Test() {
        if (EditorApplication.isPlaying) return;
        Debug.Log("== Tools/EditorTest ==");

        // 匿名对象
        var obj = new { a = 1, b = 2 };
        GetProps(obj);

        // 动态属性,使用 System.Dynamic.ExpandoObject 类实现
        dynamic dyObj = new System.Dynamic.ExpandoObject();
        dyObj.a = 3;
        dyObj.b = 4;
        GetDyProps(dyObj);

    }

    private static T CastAnonymous<T>(object anonymous, T anonymousType) {
        return (T)anonymous;
    }

    private static void GetProps(object arg) {
        // 利用泛型特性访问属性
        var obj = CastAnonymous(arg, new { a = 0, b = 0 });
        Debug.Log($"obj.a:{obj.a}, obj.b:{obj.b}"); // output: obj.a:1, obj.b:2

        // 利用反射机制访问属性
        var argType = arg.GetType();
        var a = argType.GetProperty("a").GetValue(arg);
        var b = argType.GetProperty("b").GetValue(arg);
        Debug.Log($"a:{obj.a}, b:{obj.b}"); // output: a:1, b:2
    }

    private static void GetDyProps(dynamic dyObj) {
        Debug.Log($"dyObj.a:{dyObj.a}, dyObj.b:{dyObj.b}"); // output: dyObj.a:3, dyObj.b:4
    }

}
#endif