interface

How do I convert from a slice of interface{} to a slice of my struct type in Go? [duplicate]

南楼画角 提交于 2021-02-16 15:34:14
问题 This question already has answers here : Type converting slices of interfaces (6 answers) Closed 1 year ago . func GetFromDB(tableName string, m *bson.M) interface{} { var ( __session *mgo.Session = getSession() ) //if the query arg is nil. give it the null query if m == nil { m = &bson.M{} } __result := []interface{}{} __cs_Group := __session.DB(T_dbName).C(tableName) __cs_Group.Find(m).All(&__result) return __result } call GetFromDB(T_cs_GroupName, &bson.M{"Name": "Alex"}).([]CS_Group)

Create a generic collection of derived interfaces from a collection of base interfaces

孤街浪徒 提交于 2021-02-11 18:12:25
问题 I have a base interface public interface IBase { ... } and a interface that derives from this base public interface IChild : IBase { ... } Within my code, I call a method which will return me a List<IBase> (legacy code). With this List I am trying to fill a ObservableCollection<IChild> : List<IBase> baseList= GetListofBase(); ChildList = new ObservableCollection<IChild>(); // how to fill ChildList with the contents of baseList here? I know it is not possible to cast from a base to a derived

Create a generic collection of derived interfaces from a collection of base interfaces

一曲冷凌霜 提交于 2021-02-11 18:05:29
问题 I have a base interface public interface IBase { ... } and a interface that derives from this base public interface IChild : IBase { ... } Within my code, I call a method which will return me a List<IBase> (legacy code). With this List I am trying to fill a ObservableCollection<IChild> : List<IBase> baseList= GetListofBase(); ChildList = new ObservableCollection<IChild>(); // how to fill ChildList with the contents of baseList here? I know it is not possible to cast from a base to a derived

Interface method that receives a parameter of type of class that inherited it

北战南征 提交于 2021-02-11 15:45:54
问题 I' trying to make something like this: public interface ICar { public void Update(/*something here*/); } Then two classes: public class Peugeot : ICar { public void Update(Peugeot car) { } } public class Volvo : ICar { public void Update(Volvo car) { } } How can I achieve this? 回答1: You could make ICar generic: public interface ICar<T> where T : ICar<T> { public void Update<T>(T car); } And then implement the Update methods accordingly: public class Peugeot : ICar<Peugeot> { public void

PHP Exception::getCode() contradicts Throwable interface that it implements

浪子不回头ぞ 提交于 2021-02-11 15:37:29
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

PHP Exception::getCode() contradicts Throwable interface that it implements

僤鯓⒐⒋嵵緔 提交于 2021-02-11 15:36:14
问题 I've found a contradiction I could not understand. Exception::getCode() has this definition: final public Exception::getCode ( void ) : mixed with description: Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException) but the Exception class implements Throwable interface that defines: abstract public getCode ( void ) : int So how for an example PDOException as a descendant of Exception could return string

Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class

社会主义新天地 提交于 2021-02-11 15:00:31
问题 I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below private void LoadVariables(LogDictionary dic) { foreach (var entry in dic) { _context.Variables[entry.Key] = entry.Value; } } but a NotImplementedException is thrown because I did not implement the GetEnumerator() method. Here is my wrapper class: public class LogDictionary: IDictionary<String, object> { DynamicTableEntity _dte; public LogDictionary(DynamicTableEntity dte) { _dte = dte;

Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class

ⅰ亾dé卋堺 提交于 2021-02-11 14:58:30
问题 I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below private void LoadVariables(LogDictionary dic) { foreach (var entry in dic) { _context.Variables[entry.Key] = entry.Value; } } but a NotImplementedException is thrown because I did not implement the GetEnumerator() method. Here is my wrapper class: public class LogDictionary: IDictionary<String, object> { DynamicTableEntity _dte; public LogDictionary(DynamicTableEntity dte) { _dte = dte;

Xamarin Forms custom control: implement a method in the renderer

一笑奈何 提交于 2021-02-11 14:31:08
问题 I'm trying to create an extended version of the standard XF Map object: public class RRMap: Map { public void DoSomethingOnMap() { /* ... */ } } I also created an Android renderer (iOS will come later): [assembly: ExportRenderer(typeof(RRMap), typeof(RRMapRendererAndroid))] namespace MyApp.Droid.Renderers { public class RRMapRendererAndroid : MapRenderer { public RRMapRendererAndroid(Context context) : base(context) { } protected override void OnElementChanged(Xamarin.Forms.Platform.Android

Get pointer on var obtained via interface

大城市里の小女人 提交于 2021-02-11 12:09:28
问题 In the following code var a int var b interface{} b = a fmt.Printf("%T, %T \n", a, &a) fmt.Printf("%T, %T \n", b, &b) output: int, *int int, *interface {} I would expect the type of &b to be a pointer on int. I have two questions: 1) Why is it a pointer on interface{} ? 2) How could I get a pointer on the original type ? 回答1: &b => this is the address operator applied on the variable b , whose type is interface{} . So &b will be a pointer of type *interface{} , pointing to the variable b . If