F#从C#class继承并访问受保护的字段 [英] F# Inherit from C# class + access protected fields
本文介绍了F#从C#class继承并访问受保护的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个基类创建一个新的SQL Server连接并在C#中执行一些实用工具方法,我想在F#中继承。目前我无法从F#访问C#类中的受保护字段,尽管这可以在C#中使用。
I have this base class that creates a new SQL Server connection and does some utility methods in C#, I want to inherit in F#. Currently I cannot access the protected fields in the C# class from F#, although this would work in C#.
C#abstract class
C# abstract class
public abstract class SQL
{
protected readonly SqlConnectionStringBuilder bld;
protected readonly SqlCommand cmd;
protected readonly SqlConnection conn;
protected readonly string connstring;
protected string userid;
public SQL(string server, string db)
{
bld = new SqlConnectionStringBuilder();
bld.DataSource = server;
bld.InitialCatalog = db;
bld.IntegratedSecurity = true;
connstring = bld.ConnectionString;
conn = new SqlConnection(connstring);
cmd = new SqlCommand();
GetUserID();
//Other utility methods here
}
要继承的F#代码
type Transfer ( server : string ) ( db : string ) =
inherit SQL(server, db)
let accessAbstractConn = conn. //Can't access protected field????
我错过了什么吗?我试过别名基类字段,例如this.conn也不起作用。
Have I missed something here? I have tried aliasing the base class field e.g. this.conn which also does not work.
谢谢
Richard
Thanks Richard
推荐答案
您可以为构造函数添加自标识符:
You can add a self-identifier to the constructor:
type Transfer ( server : string, db : string ) as this =
inherit SQL(server, db)
let accessAbstractConn = this.conn
或者,使用 base
关键字:
type Transfer ( server : string, db : string ) =
inherit SQL(server, db)
let accessAbstractConn = base.conn
这篇关于F#从C#class继承并访问受保护的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文