cefsharp / cefsharp/CefSharp

Feature Request - Dynamic method & property support for script object registered from .Net

Aperta
#4,658 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub
feature-request up-for-grabs
Lingua principale
C#
Stelle
10.3k
Fork
2.9k
Merge medio
5g 22h
PR unite (30g)
4

Descrizione

I have a third party lib, contains hundreds of class, each class concain 10 ~ 100+ method & property.
I try to use this in CefSharp browser, but it is unwise to create .Net wrapper for every class.
The best way is create a simple wrapper class, contain base class info,
in script, just simply use `ob.methodA(); ob.propertyA = 'a';`
in wrapper .Net class , just catch the access event, then find and run or show error like "method/propert not found".

Then i try extend class from System.Dynamic, but it does not support.
> // CefSharp/Internals/JavascriptObjectRepository.cs , line 240
> // method Register
> var type = value.GetType();
> if (type.IsPrimitive || type.BaseType.Namespace.StartsWith("System."))
> {
> throw new ArgumentException("Registering of .Net framework built in types is not supported, " +
> "create your own Object and proxy the calls if you need to access a Window/Form/Control.", "value");
> }

i don't known why, mayby security issue?

i read source code to find a way to use dynamic method or property
finally, i think there is no way to do this.
> // CefSharp/Internals/JavascriptObjectRepository.cs
> // method AnalyseObjectForBinding
> // line 670 - 699
> // it cache the method to List

>// CefSharp/Internals/JavascriptObjectRepository.cs
> // method TryCallMethod
> // line 301 - 305
> // simply check List, if not found, throw error

also there can be an ugly way to use , wrapper class create proxy method
and use in script like `ob.proxyCall('methodA', []); ob.proxyGet('propertyA')`
it's too silly.

build custom cefsharp is hard and heavy to me, so i create this issue
suggest some way to support Dynamic method & property for script object registered from .Net
here is my solution:

[1] create interface to declear .Net class need Dynamic method & property support
> // CefSharp/IDynamicMethod.cs
> interface IDynamicMethod {
> MethodInfo resolveDynamicMethod(string name);
> };
>
> // CefSharp/IDynamicProperty.cs
> interface IDynamicProperty{
> PropertyInfo resolveDynamicProperty(string name);
> };

[2] modify `CefSharp/Internals/JavascriptObjectRepository.cs`
> // method `TryCallMethod` & `TryCallMethodAsync`
> // if method not found
> // if obj implement IDynamicMethod , try get method from obj.resolveDynamicMethod
> // do some validation & check (such as security issue) then wrap .Net MethodInfo to JavascriptMethod
>
> // method `TryGetProperty` & `TrySetProperty`
> // if property not found
> // if obj implement IDynamicProperty, try get property from obj.resolveDynamicProperty
> // do some validation & check (such as security issue) then wrap .Net PropertyInfo to JavascriptProperty

then we done, it's simply change few lines.
may be can cache dynamic wrappered JavascriptMethod or JavascriptProperty
but the better way is let user decide and write extra code to cache MethodInfo and PropertyInfo.

now if user require Dynamic Method or Property
just implment IDynamicMethod or IDynamicProperty,it‘s simple , elegent and comfortable.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia da CefSharp/Internals/JavascriptObjectRepository.cs, in particolare da Register, AnalyseObjectForBinding, TryCallMethod, TryCallMethodAsync, TryGetProperty e TrySetProperty. Esamina come vengono attualmente individuati, memorizzati nella cache, convalidati e segnalati i metodi e le proprietà quando mancano. Il lavoro sarà considerato completato quando sarà definito un design deciso per la risoluzione dinamica di metodi e proprietà, incluso il comportamento di sicurezza e convalida; nell’issue non sono indicati test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
desktop
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.