C# Cefsharp 如何利用[Attribute]的把C#中的方法给到浏览器中调用

背景

"有没有遇见这样一个场景,需要注入到浏览器的类太多,又想统一管理且不遗漏,有没有什么好办法?"
”有有有,把头伸过来~“

 

解决办法

第一步:提供一个[Attribute]

既然要知道哪些类需要被浏览器,那么可以使用[Attribute]进行标记。

首先我们提供一个[Attribute],
第一个原因是 考虑到cefsharp是不支持同一个类名注入的,所以使用[Attribute]也能方便取名,以便于防止重名事情发生。

其次,如果不想要注入,可以直接移除[Attribute]

复制代码
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class JSBridgeAttribute : Attribute
{
    /// <summary>
    /// js中服务名称
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public bool IsAsync { get; set; } = true;

    public JSBridgeAttribute()
    { }

    public JSBridgeAttribute(string name, bool isAsync = true)
    {
        this.Name = name;
        this.IsAsync = isAsync;
    }
}
复制代码

有了这个,可以在你想要的class上进行标记,这样就表示,存在一个 FooService,可以调用里面的方法sum

[JSBridgeAttribute("FooService")]
public class Foo
{
    public int sum(int x,int y)
    {
        return x + y;
    }
}

 

第二步:在浏览器里面加入标记JSBridge类

因为要找到全部的标记JSBridge类,可以通过程序集反射得到,(考虑到这个过程耗时,要得到所有的类都加载完毕再打开浏览器,因此加上_initDll.Wait()

注意:存在某种情况找不到程序集,可能因为该程序集并没有在主程序中有过调用

复制代码
/// <summary>
/// 扩展全局注入js
/// </summary>
public static class BrowserExtend
{
    private static readonly List<Type> TypeCache = new List<Type>();

    private static Task _initDll;

    /// <summary>
    /// 初始化所有的DLL带JSBridgeAttribute的类
    /// </summary>
    public static Task InitDllTask()
    {
        _initDll = Task.Run(()=>
        {
            var list = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in list)
            {
                foreach (var type in GetTypesWithHelpAttribute(assembly))
                {
                    TypeCache.Add(type);
                }
            }
        });

        return _initDll;
    }

    public static void InjectBridgeObject(this BaseBrowser baseBrowser)
    {
        _initDll.Wait();

        foreach (var type in TypeCache)
        {
            var item = Activator.CreateInstance(type);
            var attribute = item.GetType().GetCustomAttributes(typeof(JSBridgeAttribute), false).FirstOrDefault() as JSBridgeAttribute;
            string name = item.GetType().Name;
            if (attribute != null)
            {
                name = attribute.Name;
            }
            baseBrowser.RegisterJsObject(name, item, true);
        }
    }

    private static IEnumerable<Type> GetTypesWithHelpAttribute(Assembly assembly)
    {
        foreach (Type type in assembly.GetTypes())
        {
            if (type.GetCustomAttributes(typeof(JSBridgeAttribute), true).Length > 0)
            {
                yield return type;
            }
        }
    }
}
复制代码

最后,在App.xaml.cs或者其他地方,使用 BrowserExtend.InitDllTask();

posted @   樱花落舞  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-11-25 2014江西理工大学C语言程序竞赛高级组
2015-11-25 Testing Round #12 A
2015-11-25 湖南工业大学创新实验室2015年新生赛(一)1003(重开)
2015-11-25 湖南工业大学创新实验室2015年新生赛(一)1009(重开)
2015-11-25 湖南工业大学创新实验室2015年新生赛(一)1006(重开)
2015-11-25 湖南工业大学创新实验室2015年新生赛(一)1005(重开)
2015-11-25 湖南工业大学创新实验室2015年新生赛(一)1002(重开)
点击右上角即可分享
微信分享提示